binmode in perl -p oneliner Optionen

binmode in perl -p oneliner Optionen

am 26.05.2011 15:04:27 von Thomas Liebezeit

Hello,

I'm triying to do some substitutions on an pdf file.


perl -p -i~ -w -e "s/A/B/;" file.pdf

This works as intended, except: perl adds 0x0D (Windows \n) :-/
as a HEX diff shows.
How can I work around this? Is there something like binmode()?

cheers
Thomas




--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: binmode in perl -p oneliner Optionen

am 28.05.2011 22:00:35 von jwkrahn

Thomas Liebezeit wrote:
> Hello,

Hello,

> I'm triying to do some substitutions on an pdf file.
>
>
> perl -p -i~ -w -e "s/A/B/;" file.pdf
>
> This works as intended, except: perl adds 0x0D (Windows \n) :-/
> as a HEX diff shows.
> How can I work around this? Is there something like binmode()?

You should be able to do this with the open module:

perl -Mopen=:bytes -i~ -pe "s/A/B/" file.pdf

Or it should work in slurp mode if the files are small enough to fit in
memory:

perl -i~ -0777pe "s/A/B/" file.pdf


BTW your substitution operator replaces only the first 'A' it finds with
'B'. If you want to replace all 'A's with 'B's then you have to use the
/g option on the substitution: s/A/B/g. And it you are only really
replacing one character with another then you should probably use
transliteration instead: tr/A/B/.



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: binmode in perl -p oneliner Optionen

am 28.05.2011 22:43:09 von Uri Guttman

>>>>> "TL" == Thomas Liebezeit writes:

TL> I'm triying to do some substitutions on an pdf file.

TL> perl -p -i~ -w -e "s/A/B/;" file.pdf

TL> This works as intended, except: perl adds 0x0D (Windows \n) :-/
TL> as a HEX diff shows.
TL> How can I work around this? Is there something like binmode()?

there isn't a binmode option for the command line. you would need to
call binmode from the oneliner and that makes it more than a typical one
liner. write a real but short script to do this. in fact, you can use a
new version of File::Slurp that can do that in one call. check this out
(untested):


use File::Slurp qw( edit_file ) ;

my $file = shift or die "missing file argument" ;

edit_file { s/A/B/g } $file, { binmode => ':raw' } ;


you could even reduce that to a one liner if you must (also untested):

perl -MFile::Slurp=edit_file -e 'edit_file { s/A/B/g } shift, { binmode => ":raw" }' foo.pdf


note that i added the /g option to the s/// op to replace all A with B
in the pdf. you did it on a line by line basis and it would only change
the first occurance in a line. alter to your needs.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: binmode in perl -p oneliner Optionen

am 28.05.2011 22:47:59 von Uri Guttman

>>>>> "JWK" == John W Krahn writes:

JWK> perl -i~ -0777pe "s/A/B/" file.pdf

that won't help as line ending hacking occurs on all text files on
winblows if you use stdio which that does. it isn't only on line
oriented operations. your open idea would work here if it worked on the
other one.

JWK> BTW your substitution operator replaces only the first 'A' it finds
JWK> with 'B'. If you want to replace all 'A's with 'B's then you have to
JWK> use the /g option on the substitution: s/A/B/g. And it you are only
JWK> really replacing one character with another then you should probably
JWK> use transliteration instead: tr/A/B/.

i noted that in my other post. the OP's would do one substitution per line
with the normal -p mode but only one globally with the -0777 slurp
mode.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: binmode in perl -p oneliner Optionen

am 29.05.2011 10:38:11 von Thomas Liebezeit

Thank you, John and Uri.

I got a hint to use -e "binmode ARGVOUT" and it did the trick for me!

JWK> BTW your substitution operator replaces only the first 'A' it
finds
JWK> with 'B'. If you want to replace all 'A's with 'B's then you
have to
JWK> use the /g option on the substitution: s/A/B/g. And it you are
only
JWK> really replacing one character with another then you should
probably
JWK> use transliteration instead: tr/A/B/.
Thanks, but A and B were only placeholders here. Nevertheless, your
remark for /g was very welcome ;-)

Thomas


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/