On Friday 09 Apr 2010 18:31:31 Niko zuna wrote:
> Hi!
>
> I made this short script in order to see if there is posible to run
> "modified" unix commands from a Perl script.
>
> #!/usr/bin/perl
>
>
>
> use
> strict;
>
> use
> warnings;
>
>
>
> my $load = `uptime | awk '{print $8 $9 $10 $11
> $12}'`;
>
>
> print "$load\n";
>
> when I am trying to run this skript, I get the whole uptime output, not
> only the load averages: x x x
> And this Error msg:
>
> Use of uninitialized value $8 in concatenation (.) or string at
> ./test.plline 6. Use of uninitialized value $9 in concatenation (.) or
> string at ./test.plline 6. Use of uninitialized value $10 in concatenation
> (.) or string at
> ./test.plline 6.
> Use of uninitialized value $11 in concatenation (.) or string at
> ./test.plline 6.
> Use of uninitialized value $12 in concatenation (.) or string at
> ./test.plline 6.
> 17:20:46 up 7 days, 6:52, 11 users, load average: 0.00, 0.00, 0.00
>
That's because Perl interpolates the $... inside the `..`. You need to escape
them with \$ (for Perl - not for the shell in that case) or use q{...} and
interpolate that (Perl does not do double-interpolation). Note that calling
into Unix commands is fine for experimentation, but should be avoided as much
as possible in production code because it's non-portable and prone for errors.
For an extreme example see:
http://thedailywtf.com/Articles/The_UNIX_Philosophy.aspx
> Please help!
Regards,
Shlomi Fish
--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
"Humanity" - Parody of Modern Life - http://shlom.in/humanity
Deletionists delete Wikipedia articles that they consider lame.
Chuck Norris deletes deletionists whom he considers lame.
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/