Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: Problem in executing the perl oneliner within the script

by perladdict (Chaplain)
on Feb 25, 2014 at 12:26 UTC ( #1076134=note: print w/ replies, xml ) Need Help??


in reply to Re: Problem in executing the perl oneliner within the script
in thread Problem in executing the perl oneliner within the script

Thanks Kcott, based on your detail description for the error message i modified the command as below.
In our server there is no stricts and warning in stalled @INC hence unable to use this in my test script.

@reports = `ls *.doc`; foreach $file(@reports) { $cmd=`perl -ni -e 'print;print "VALUE=KEY\n" if \$\.== 3;' $file` }
The above code is working fine now, with this i can able to add a line to around 1400 report files.


Comment on Re^2: Problem in executing the perl oneliner within the script
Download Code
Re^3: Problem in executing the perl oneliner within the script
by 2teez (Priest) on Feb 25, 2014 at 13:32 UTC

    Hi,
    "..In our server there is no stricts and warning in stalled @INC hence unable to use this in my test script..."
    strict and warnings comes installed with your perl installation. strict since perl 5 and warnings since perl v5.6.0. So if your perl version is newer than the ones i mentioned then you have these pragma installed.
    Just use like so

    use warnings; use strict;
    in your perl scripts.
    Moreso, I wonder why you are using a oneliner perl with a backtick while you can do all that very simply and clearly like so:
    *
    use warnings; use strict; for my $file ( glob("*.pl") ) { open my $fh, '<', $file or die $!; while ( defined( $_ = <$fh> ) ) { print $_; print "adding value=key\n" if $. == 3; } }
    * Update

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re^3: Problem in executing the perl oneliner within the script (strict and warnings)
by kcott (Abbot) on Feb 25, 2014 at 16:09 UTC
    "In our server there is no stricts and warning in stalled @INC hence unable to use this in my test script."

    It's not "stricts", it's "strict". I have no "stricts" either:

    $ perl -Mstricts -e 1 Can't locate stricts.pm in @INC (you may need to install the stricts m +odule) (@INC contains: ...). BEGIN failed--compilation aborted.

    It's not "warning", it's "warnings". I have no "warning" either:

    Can't locate warning.pm in @INC (you may need to install the warning m +odule) (@INC contains: ...). BEGIN failed--compilation aborted.

    Please try these from your command line and report any output:

    perl -Mstrict -e 1
    perl -Mwarnings -e 1

    If you get no output from those, you have both strict and warnings installed. However, if either produces any output, please run this and report all output:

    perl -v

    -- Ken

Re^3: Problem in executing the perl oneliner within the script (calling perl from perl)
by kcott (Abbot) on Feb 25, 2014 at 16:57 UTC
    @reports = `ls *.doc`; foreach $file(@reports) { $cmd=`perl -ni -e 'print;print "VALUE=KEY\n" if \$\.== 3;' $file` }

    The above code is working fine now, with this i can able to add a line to around 1400 report files.

    That script will be creating ~1400 perl processes. You've been repeatedly advised about this yet you continue to do it.

    This script performs exactly the same function but only uses one perl process:

    #!/usr/bin/env perl -i use strict; use warnings; while (<>) { print; print "VALUE=KEY\n" if $. == 3; $. = 0 if eof; }

    I suggest you read "perlrun - how to execute the Perl interpreter". Amongst other things, this will tell you about the Command Switches: perhaps lack of knowledge in this area is why you're coding your script the way you are.

    I put the above code in pm_1076103.pl and created three *.doc files:

    $ ls -al *.doc -rw-r--r-- 1 ken staff 52 26 Feb 03:20 pm_1076103_1.doc -rw-r--r-- 1 ken staff 52 26 Feb 03:22 pm_1076103_2.doc -rw-r--r-- 1 ken staff 52 26 Feb 03:22 pm_1076103_3.doc $ cat pm_1076103_1.doc Doc 1 Line 1 Doc 1 Line 2 Doc 1 Line 3 Doc 1 Line 4 $ cat pm_1076103_2.doc Doc 2 Line 1 Doc 2 Line 2 Doc 2 Line 3 Doc 2 Line 4 $ cat pm_1076103_3.doc Doc 3 Line 1 Doc 3 Line 2 Doc 3 Line 3 Doc 3 Line 4

    I then ran the script like this:

    $ pm_1076103.pl *.doc

    Here's what the *.doc files now look like:

    $ cat pm_1076103_1.doc Doc 1 Line 1 Doc 1 Line 2 Doc 1 Line 3 VALUE=KEY Doc 1 Line 4 $ cat pm_1076103_2.doc Doc 2 Line 1 Doc 2 Line 2 Doc 2 Line 3 VALUE=KEY Doc 2 Line 4 $ cat pm_1076103_3.doc Doc 3 Line 1 Doc 3 Line 2 Doc 3 Line 3 VALUE=KEY Doc 3 Line 4

    -- Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1076134]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2014-10-22 22:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (122 votes), past polls