Re: Storing Output file.
Thanks a lot Jonathan, i will set the env properly then i will try. -Frank On Fri, Jan 29, 2016 at 3:36 PM, Jonathan Harris wrote: > Hi Frank, > Please would you remember to Reply All to the list as well? > > It just seems that the path is not included in @INC > You can check on the command line: > > perl -e "print qq(@INC)" > > I can't tell how you installed Perl and cpan but that's the result! > > Anyways, it's easily fixed. > > At the start of the script, use: > > use lib 'C:\Perl64\cpan\build'; > use File::Slurp qw( :edit ); > > However, this would have to be added to every script. > If the path is an issue for all scripts, then it would be better to make > the change permanent. > There are good instructions here to adding the Environment Variable: > > > http://perlmaven.com/how-to-change-inc-to-find-perl-modules-in-non-standard-locations > > Good luck! > Jon > > > On Fri, Jan 29, 2016 at 4:08 AM, Frank Vino wrote: > >> Hi Jonathan, >> >> I am using Windows OS i tried but i got some error message i am attaching >> the message here >> >> *Output:* >> >> C:\Users\Franklin_Lawerence\Desktop\perl>arrarsize.pl >> Can't locate File/Slurp.pm in @INC (@INC contains: C:/Perl64/site/lib >> C:/Perl64/lib .) at C:\Users\Franklin_Lawerence\Desktop\perl\arrarsize.pl >> line 5. >> BEGIN failed--compilation aborted at >> C:\Users\Franklin_Lawerence\Desktop\perl\arrarsize.pl line 5. >> >> C:\Users\Franklin_Lawerence\Desktop\perl> >> >> >> *File-Slurp installed in below Program files folder:* >> >> C:\Perl64\cpan\build\File-Slurp-.19-_tH9hN >> >> On Thu, Jan 28, 2016 at 11:27 PM, Jonathan Harris via beginners < >> beginners@perl.org> wrote: >> >>> Hi, >>> I found that this works, assuming that the module is installed. >>> >>> #!/usr/bin/perl >>> use warnings; >>> use strict; >>> use File::Slurp qw ( :edit ); >>> # >>> my $file_to_edit = 'path-to-file.txt'; >>> # >>> my $word_to_edit = "Debug"; >>> my $new_word = "Error"; >>> # >>> edit_file { s/$word_to_edit/$new_word/g } ( $file_to_edit ); >>> # >>> >>> This will work if you have the word Debug, Debug_ etc etc >>> You can just use s/Debug/Error/g but I like the variables as it allows >>> flexibility if the script was to expand to further uses >>> >>> Hope that helps, >>> Jon >>> >>> >>> On Thu, Jan 28, 2016 at 3:41 PM, Jim Gibson wrote: >>> > On Jan 28, 2016, at 1:37 AM, Frank Larry wrote: > > Hi Team, > > could you please let me? i have a file which contains "Debug", i would like to replace debug to "Error", when i ran the below program the out showing Error message but how to save the output with new changes. Could you please tell me how to fix it? The way to do this within a larger Perl program is to open a new output file, copy all of the possibly-modified lines to this file. Then you can rename the new file to the same name as the old file, and perhaps rename the old file as well and keep it around as a backup. > > open(FILE, ">>> The three-argument version of open is preferred here, and let’s put the file name in a variable and use a lexical variable for the file handle (untested): my $filename = ‘filter.txt’; open( my $in, ‘<‘, $filename ) or die(“Can’t open $filename for reading: $!”); # create a new file my $newfile = $filename . ‘.new’; open( my $out, ‘>’, $newfile ) or die(“Can’t create $newfile: $!”); > > while($line = ){ while( $line = <$in> ) { > >print "Before substituting: ", $line ,"\n"; > $line =~ s/Debug/Error/g; > print "After substituting : ", $line , "\n”; print $out $line; > > } > > close(FILE); close($in); close($out) or die(“Error writing to output file $newfile: $!”); # rename the old file my $savefile = $filename . ‘.sav’; rename $filename, $savefile; # rename the new file rename $newfile, $filename; Jim Gibson j...@gibson.org -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/ >>> >> >
Re: Storing Output file.
Hi Team, I tried it was working. -Franky On Fri, Jan 29, 2016 at 8:21 AM, Frank Larry wrote: > Thank you so much for the solution you people provided. :) > > Warm Regards, > -Franky > > On Thu, Jan 28, 2016 at 8:49 PM, Malisetti Ram Murthy < > malisettirammur...@gmail.com> wrote: > >> Hi Frank, >> >> Invoke below command from your perl program to replace the string "Debug" >> to "Error": >> >> sed -i -e '/Debug/ r filter.txt' -e s/Debug/Error/g filter_replaced.txt >> >> Above solution is provided as per my knowledge, please post if there is >> any other solutions for this. >> >> Thanks, >> Ram Murthy >> >> >> On Thu, Jan 28, 2016 at 8:29 PM, Logust Yu via beginners < >> beginners@perl.org> wrote: >> >>> You can probably achieve this easily with 'sed' on bash. >>> >>> On 28 Jan 2016, at 09:37, Frank Larry wrote: >>> >>> Hi Team, >>> >>> could you please let me? i have a file which contains "Debug", i would >>> like to replace debug to "Error", when i ran the below program the out >>> showing Error message but how to save the output with new changes. Could >>> you please tell me how to fix it? >>> >>> open(FILE, ">> >>> while($line = ){ >>>print "Before substituting: ", $line ,"\n"; >>> $line =~ s/Debug/Error/g; >>> print "After substituting : ", $line , "\n"; >>> } >>> >>> close(FILE); >>> >>> >>> -Franky >>> >>> >> >
Re: Storing Output file.
Hi Shlomi, Thanks for that pointer. I read your linked pages and it doesn't seem that there is yet a solution as simple as the 'edit_file' method. Shame as it was so handy, but the arguments against it are quite compelling. @Frank - looks like the original scripting with printing to file handles is the immediate future. Thanks, Jonathan On Fri, Jan 29, 2016 at 10:45 AM, Shlomi Fish wrote: > Hi Jonathan, > > On Thu, 28 Jan 2016 17:57:19 + > Jonathan Harris via beginners wrote: > > > Hi, > > I found that this works, assuming that the module is installed. > > > > #!/usr/bin/perl > > use warnings; > > use strict; > > use File::Slurp qw ( :edit ); > > using File::Slurp is no longer recommended: > > > http://blogs.perl.org/users/leon_timmermans/2015/08/fileslurp-is-broken-and-wrong.html > > I don't know how many of the listed alternatives have equivalents to the > edit > functions, but see my issue and branch for Path::Tiny about it: > > * https://github.com/dagolden/Path-Tiny/issues/161 > > * > https://github.com/shlomif/Path-Tiny/tree/edit-file-methods--a-la-File-Slurp > > Regards, > > Shlomi Fish > > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > >
Re: unexpected escaping
On Fri, Jan 29, 2016 at 2:48 AM, Charles DeRykus wrote: > On Fri, Jan 29, 2016 at 12:39 AM, Jorge Almeida wrote: >> Can someone help me to understand this? >> >> #!/usr/bin/perl >> use strict; >> use warnings; >> my $s='\\n'; >> print $s; >> >> >> Output: >> \n >> >> Expected output: >> \\n >> >> >> Jorge Almeida >> > > > From: perldoc perlop > > q/STRING/ >'STRING' >A single-quoted, literal string. A backslash represents a >backslash unless followed by the delimiter or another backslash, in >which case the delimiter or backslash is interpolated > Thank you, this section of the documentation is clear enough. Unfortunately, I never found any other text about the difference between single and double quotes that alerts to this violation of the principle of least surprise. My fault for not relying exclusively on the official documentation. Thanks again, the mystery is explained. Jorge -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: unexpected escaping
On Fri, Jan 29, 2016 at 12:39 AM, Jorge Almeida wrote: > Can someone help me to understand this? > > #!/usr/bin/perl > use strict; > use warnings; > my $s='\\n'; > print $s; > > > Output: > \n > > Expected output: > \\n > > > Jorge Almeida > From: perldoc perlop q/STRING/ 'STRING' A single-quoted, literal string. A backslash represents a backslash unless followed by the delimiter or another backslash, in which case the delimiter or backslash is interpolated so, you'll need: my $s = 'n' -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Storing Output file.
Hi Jonathan, On Thu, 28 Jan 2016 17:57:19 + Jonathan Harris via beginners wrote: > Hi, > I found that this works, assuming that the module is installed. > > #!/usr/bin/perl > use warnings; > use strict; > use File::Slurp qw ( :edit ); using File::Slurp is no longer recommended: http://blogs.perl.org/users/leon_timmermans/2015/08/fileslurp-is-broken-and-wrong.html I don't know how many of the listed alternatives have equivalents to the edit functions, but see my issue and branch for Path::Tiny about it: * https://github.com/dagolden/Path-Tiny/issues/161 * https://github.com/shlomif/Path-Tiny/tree/edit-file-methods--a-la-File-Slurp Regards, Shlomi Fish -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Storing Output file.
On Thu, 28 Jan 2016 07:41:15 -0800 Jim Gibson wrote: > > On Jan 28, 2016, at 1:37 AM, Frank Larry wrote: > > > > Hi Team, > > > > could you please let me? i have a file which contains "Debug", i would > > like to replace debug to "Error", when i ran the below program the out > > showing Error message but how to save the output with new changes. Could > > you please tell me how to fix it? > > The way to do this within a larger Perl program is to open a new output file, > copy all of the possibly-modified lines to this file. Then you can rename the > new file to the same name as the old file, and perhaps rename the old file as > well and keep it around as a backup. > > > > > open(FILE, " > The three-argument version of open is preferred here, and let’s put the file > name in a variable and use a lexical variable for the file handle (untested): > > my $filename = ‘filter.txt’; > open( my $in, ‘<‘, $filename ) or die(“Can’t open $filename for reading: $!”); > > # create a new file > my $newfile = $filename . ‘.new’; > open( my $out, ‘>’, $newfile ) or die(“Can’t create $newfile: $!”); > > > > > while($line = ){ > > while( $line = <$in> ) { > In addition to all that: 1. Add "use strict;" and "use warnings;" at the beginning. 2. «while ($line = <$in>)» should be «while (my $line = <$in>)» For more information see: http://perl-begin.org/tutorials/bad-elements/ > > > >print "Before substituting: ", $line ,"\n"; > > $line =~ s/Debug/Error/g; > > print "After substituting : ", $line , "\n”; > > print $out $line; This is easy to confuse with: print $out, $line; As a result it is preferable to do: print {$out} $line; Or in new versions enough of perl : $out->print($line); Finally, there's also http://perldoc.perl.org/autodie.html . Regards, Shlomi Fish -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Storing Output file.
Hi Frank, Please would you remember to Reply All to the list as well? It just seems that the path is not included in @INC You can check on the command line: perl -e "print qq(@INC)" I can't tell how you installed Perl and cpan but that's the result! Anyways, it's easily fixed. At the start of the script, use: use lib 'C:\Perl64\cpan\build'; use File::Slurp qw( :edit ); However, this would have to be added to every script. If the path is an issue for all scripts, then it would be better to make the change permanent. There are good instructions here to adding the Environment Variable: http://perlmaven.com/how-to-change-inc-to-find-perl-modules-in-non-standard-locations Good luck! Jon On Fri, Jan 29, 2016 at 4:08 AM, Frank Vino wrote: > Hi Jonathan, > > I am using Windows OS i tried but i got some error message i am attaching > the message here > > *Output:* > > C:\Users\Franklin_Lawerence\Desktop\perl>arrarsize.pl > Can't locate File/Slurp.pm in @INC (@INC contains: C:/Perl64/site/lib > C:/Perl64/lib .) at C:\Users\Franklin_Lawerence\Desktop\perl\arrarsize.pl > line 5. > BEGIN failed--compilation aborted at > C:\Users\Franklin_Lawerence\Desktop\perl\arrarsize.pl line 5. > > C:\Users\Franklin_Lawerence\Desktop\perl> > > > *File-Slurp installed in below Program files folder:* > > C:\Perl64\cpan\build\File-Slurp-.19-_tH9hN > > On Thu, Jan 28, 2016 at 11:27 PM, Jonathan Harris via beginners < > beginners@perl.org> wrote: > >> Hi, >> I found that this works, assuming that the module is installed. >> >> #!/usr/bin/perl >> use warnings; >> use strict; >> use File::Slurp qw ( :edit ); >> # >> my $file_to_edit = 'path-to-file.txt'; >> # >> my $word_to_edit = "Debug"; >> my $new_word = "Error"; >> # >> edit_file { s/$word_to_edit/$new_word/g } ( $file_to_edit ); >> # >> >> This will work if you have the word Debug, Debug_ etc etc >> You can just use s/Debug/Error/g but I like the variables as it allows >> flexibility if the script was to expand to further uses >> >> Hope that helps, >> Jon >> >> >> On Thu, Jan 28, 2016 at 3:41 PM, Jim Gibson wrote: >> >>> >>> > On Jan 28, 2016, at 1:37 AM, Frank Larry >>> wrote: >>> > >>> > Hi Team, >>> > >>> > could you please let me? i have a file which contains "Debug", i >>> would like to replace debug to "Error", when i ran the below program the >>> out showing Error message but how to save the output with new changes. >>> Could you please tell me how to fix it? >>> >>> The way to do this within a larger Perl program is to open a new output >>> file, copy all of the possibly-modified lines to this file. Then you can >>> rename the new file to the same name as the old file, and perhaps rename >>> the old file as well and keep it around as a backup. >>> >>> > >>> > open(FILE, ">> >>> The three-argument version of open is preferred here, and let’s put the >>> file name in a variable and use a lexical variable for the file handle >>> (untested): >>> >>> my $filename = ‘filter.txt’; >>> open( my $in, ‘<‘, $filename ) or die(“Can’t open $filename for reading: >>> $!”); >>> >>> # create a new file >>> my $newfile = $filename . ‘.new’; >>> open( my $out, ‘>’, $newfile ) or die(“Can’t create $newfile: $!”); >>> >>> > >>> > while($line = ){ >>> >>> while( $line = <$in> ) { >>> >>> > >>> >print "Before substituting: ", $line ,"\n"; >>> > $line =~ s/Debug/Error/g; >>> > print "After substituting : ", $line , "\n”; >>> >>> print $out $line; >>> > >>> > } >>> > >>> > close(FILE); >>> >>> close($in); >>> close($out) or die(“Error writing to output file $newfile: $!”); >>> >>> # rename the old file >>> my $savefile = $filename . ‘.sav’; >>> rename $filename, $savefile; >>> >>> # rename the new file >>> rename $newfile, $filename; >>> >>> Jim Gibson >>> j...@gibson.org >>> >>> >>> >>> >>> -- >>> To unsubscribe, e-mail: beginners-unsubscr...@perl.org >>> For additional commands, e-mail: beginners-h...@perl.org >>> http://learn.perl.org/ >>> >>> >>> >> >
unexpected escaping
Can someone help me to understand this? #!/usr/bin/perl use strict; use warnings; my $s='\\n'; print $s; Output: \n Expected output: \\n TIA Jorge Almeida -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/