Operating System - HP-UX
1821188 Members
3388 Online
109631 Solutions
New Discussion юеВ

Re: Nesting the back tick operator in Perl

 
SOLVED
Go to solution
Daavid Turnbull
Frequent Advisor

Nesting the back tick operator in Perl

There has got to be a way of doing this.

Basically I want to do something like:

`mailx -s "Subject" $dest < \`tail -20 $file\` ';

The escaping of the back ticks did not work as I expected and I get an error like:
_____________

Bareword found where operator expected at ./SigCounter.pl line 53, near "`mailx -s "$msg" $dest < /`tail"
(Missing operator before tail?)
syntax error at ./SigCounter.pl line 53, near "`mailx -s "$msg" $dest < /`tail "
Scalar found where operator expected at ./SigCounter.pl line 53, at end of line
(Missing operator before ?)
_________

I got around it by redirecting the output of the tail to a file, using that file and deleting it but I figure there has to be around this.
Behold the turtle for he makes not progress unless he pokes his head out.
4 REPLIES 4
Ermin Borovac
Honored Contributor

Re: Nesting the back tick operator in Perl

Have you tried using pipe?

`tail -20 $file | mailx -s "Subject" $dest`
H.Merijn Brand (procura
Honored Contributor
Solution

Re: Nesting the back tick operator in Perl

Backticks also don't nest in shells

some perl ways. first the answer of Ermin is very good:

qx{tail -20 $file | mailx -s "Subject" $dest};

if you want to do it completely in perl

open my $f, "< $file" or die "Cannot open input: $!";
my @in;
while (<$f>) { $in[$. % 20] = $_ }
close $f;
my $in = ++$. % 20;
open my $m, "| mailx -s Subject $dest";
print $m @in[$in..$#in],@in[0..($in-1)];
close $m;

Or even better, use a mail module:

use Mail::Sendmail;
open my $f, "< $file" or die "Cannot open input: $!";
my @in;
while (<$f>) { $in[$. % 20] = $_ }
close $f;
my $in = ++$. % 20;
sendmail ({To => $dest, From => 'me@here.com', Message => join "", @in[$in..$#in],@in[0..($in-1)]}) or die $Mail::Sendmail::error;

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Daavid Turnbull
Frequent Advisor

Re: Nesting the back tick operator in Perl

The pipe solution works and is simple which I like.

I am a little surprised that it appears that you cannot pass a back tick pair to a shell.

It comes as no surprise that they cannot be nested in shells.

I always enjoy coding in Perl. I think it is the JAPH thing.
Behold the turtle for he makes not progress unless he pokes his head out.
Dennis Handly
Acclaimed Contributor

Re: Nesting the back tick operator in Perl

>I am a little surprised that it appears that you cannot pass a back tick pair to a shell.

 

That's why you don't use these archaic back quotes.  In a real shell you use $(), which nests.