# Perl Scripting

**URL:** <https://community.unix.com/t/perl-scripting/160310>\
**Category:** Shell Programming and Scripting\
**Created:** [March 16, 2006, 9:07am UTC](https://community.unix.com/t/perl-scripting/160310 "2006-03-16T09:07:24Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![vaibhav](https://community.unix.com/letter_avatar/vaibhav/32/6_22734026db12803ebb3e059622ac3534.png) [@vaibhav](https://community.unix.com/u/vaibhav)\
**Post date:** [March 16, 2006, 9:07am UTC](https://community.unix.com/t/perl-scripting/160310/1 "2006-03-16T09:07:24Z")

</div>

Hi,  
I want to use the mailx command or sendmail command in perl script.  
I am trying this one and getting error:

system "mailx -s \"Test mail\" $ar1";

Error:

The flags you gave are used only when sending mail.

---

<div class="post-metadata">

**Author:** ![gsatch](https://community.unix.com/letter_avatar/gsatch/32/6_22734026db12803ebb3e059622ac3534.png) [@gsatch](https://community.unix.com/u/gsatch)\
**Post date:** [March 16, 2006, 10:36am UTC](https://community.unix.com/t/perl-scripting/160310/2 "2006-03-16T10:36:56Z")

</div>

Vaibhav,

Try this instead:

```
 open \(MAIL,"|mailx -s &lt;subject&gt; &lt;email addresses&gt;"\);
 print MAIL "Mail line 1\\n";
 print MAIL "Mail line 2\\n";
 ...
 close \(MAIL\);

```

Greg

---

<div class="post-metadata">

**Author:** ![buffoonix](https://community.unix.com/letter_avatar/buffoonix/32/6_22734026db12803ebb3e059622ac3534.png) [@buffoonix](https://community.unix.com/u/buffoonix)\
**Post date:** [March 16, 2006, 7:20pm UTC](https://community.unix.com/t/perl-scripting/160310/3 "2006-03-16T19:20:04Z")

</div>

You could use the pipe open() of Perl as showed by Greg.  
(see perldoc perlopentut, perldoc perlipc)  
However, if you cannot trust the caller's input I would either  
do a forking open with untainting in the child's block,  
or rather use modules like Mail::Send or Mail::Mailer etc.

> **[Mail::Send - metacpan.org](https://metacpan.org/release/MARKOV/MailTools-1.74/view/Mail/Send.pm)**
>
> Various ancient e-mail related modules

---

<div class="post-metadata">

**Author:** ![vaibhav](https://community.unix.com/letter_avatar/vaibhav/32/6_22734026db12803ebb3e059622ac3534.png) [@vaibhav](https://community.unix.com/u/vaibhav)\
**Post date:** [March 20, 2006, 10:29am UTC](https://community.unix.com/t/perl-scripting/160310/4 "2006-03-20T10:29:38Z")

</div>

Hi Greg 10X,  
your advice worked regarding the perl script. 🙂

---

<div class="post-metadata">

**Author:** ![vaibhav](https://community.unix.com/letter_avatar/vaibhav/32/6_22734026db12803ebb3e059622ac3534.png) [@vaibhav](https://community.unix.com/u/vaibhav)\
**Post date:** [May 8, 2006, 7:32am UTC](https://community.unix.com/t/perl-scripting/160310/5 "2006-05-08T07:32:42Z")

</div>

Hi All,

How can i use "echo" command in perl?

Let suppose i set one variable $usr\_info = abc /def/ghi/abc

now want get the out put /def/ghi/abc in variable my\_path using command like this

$my\_path = `echo $usr_info | cut -f2 -d" "`

but i am getting error related to "|" "pipe" when i run the script.

---

<div class="post-metadata">

**Author:** ![cbkihong](https://community.unix.com/letter_avatar/cbkihong/32/6_22734026db12803ebb3e059622ac3534.png) [@cbkihong](https://community.unix.com/u/cbkihong)\
**Post date:** [May 8, 2006, 10:53am UTC](https://community.unix.com/t/perl-scripting/160310/6 "2006-05-08T10:53:00Z")

</div>

Can't reproduce.

Maybe post the relevant lines and the exact output (inc. error message)?

---

<div class="post-metadata">

**Author:** ![buffoonix](https://community.unix.com/letter_avatar/buffoonix/32/6_22734026db12803ebb3e059622ac3534.png) [@buffoonix](https://community.unix.com/u/buffoonix)\
**Post date:** [May 8, 2006, 2:28pm UTC](https://community.unix.com/t/perl-scripting/160310/7 "2006-05-08T14:28:16Z")

</div>

You are obviously mixing shell and Perl variables here.  
Generally you should avoid this mixture because Perl is powerful enough  
to get rid of all shell escapes, and Perl has built-ins of almost all Unix syscalls  
which render them completely redundant.  
Not only degrades this the performance of your Perl program  
(though this is seldom an issue for the quick and dirty hack),  
but it also makes your programs more insecure,  
which indeed is relevant for e.g. CGIs or server programs.  
(n.b. if you can afford the time you should read [perldoc perlsec](http://perldoc.perl.org/perlsec.html) )

But if you are reluctant to rewrite your shell script wrapper into your Perl code  
then you could either pass shell variables to the called Perl script  
e.g.

```plaintext
$ (user_info="abc /def/ghi/abc"; perl -le '$my_path=$ARGV[1];print $my_path' $user_info)
/def/ghi/abc

```

or you could access the shell variables via the %ENV hash,  
provided you export them in your shell wrapper script  
e.g.

```plaintext
$ (user_info="abc /def/ghi/abc" perl -le '$my_path=$ENV{user_info};print $my_path' $user_info)
abc /def/ghi/abc

```

---

<div class="post-metadata">

**Author:** ![buffoonix](https://community.unix.com/letter_avatar/buffoonix/32/6_22734026db12803ebb3e059622ac3534.png) [@buffoonix](https://community.unix.com/u/buffoonix)\
**Post date:** [May 8, 2006, 2:32pm UTC](https://community.unix.com/t/perl-scripting/160310/8 "2006-05-08T14:32:05Z")

</div>

Oops, I made a flaw.  
Of course you would further need to split or pattern match the value of $ENV{user\_info}

```nohighlight
$ (user_info="abc /def/ghi/abc" perl -le '$my_path=(split/\s+/,$ENV{user_info})[1];print $my_path' $user_info)
/def/ghi/abc

```

---

<div class="post-metadata">

**Author:** ![vaibhav](https://community.unix.com/letter_avatar/vaibhav/32/6_22734026db12803ebb3e059622ac3534.png) [@vaibhav](https://community.unix.com/u/vaibhav)\
**Post date:** [March 26, 2007, 5:26am UTC](https://community.unix.com/t/perl-scripting/160310/9 "2007-03-26T05:26:27Z")

</div>

Hi There,  
I am getting a vieared problem while building my code: problem is as follow:  
/usr/lib/dld.sl: Call to mmap() failed - TEXT /oravl01/oracle/9.2.0.7/lib32/libclntsh.sl.9.0  
/usr/lib/dld.sl: Not enough space  
Abort (core dumped)  
any body have idea how to resolve it?

---

<div class="post-metadata">

**Author:** ![vaibhav](https://community.unix.com/letter_avatar/vaibhav/32/6_22734026db12803ebb3e059622ac3534.png) [@vaibhav](https://community.unix.com/u/vaibhav)\
**Post date:** [March 27, 2007, 5:11am UTC](https://community.unix.com/t/perl-scripting/160310/10 "2007-03-27T05:11:26Z")

</div>

Hi There,

I am writing a perl script in which i want to do something like this:

for $line ( `cat /etc/passwd | grep 'sbcuser'`){  
print "1. Hi $line \n";  
$usr=`cut -f1 -d':' $line`;  
$fs=`cut -f6 -d':' $line | xargs dirname`;  
%user=( "$usr" =\> $fs);  
}

but i am getting error while executing this peace of code.

Any idea what i am missing?

---

<div class="post-metadata">

**Author:** ![anbu23](https://community.unix.com/user_avatar/community.unix.com/anbu23/32/351_2.png) [@anbu23](https://community.unix.com/u/anbu23)\
**Post date:** [March 27, 2007, 5:32am UTC](https://community.unix.com/t/perl-scripting/160310/11 "2007-03-27T05:32:39Z")

</div>

> [@vaibhav](#):
>
> Hi There,
> 
> I am writing a perl script in which i want to do something like this:
> 
> for $line ( `cat /etc/passwd | grep 'sbcuser'`){  
> print "1. Hi $line \n";  
> $usr=`cut -f1 -d':' $line`;  
> $fs=`cut -f6 -d':' $line | xargs dirname`;  
> %user=( "$usr" =\> $fs);  
> }
> 
> but i am getting error while executing this peace of code.
> 
> Any idea what i am missing?

```plaintext
#!/usr/bin/perl
open FH , "/etc/passwd" ;
while( <FH> ){
if( /^sbcuser/ )
{
print "1. Hi $_ \n";
@F=split /:/;
$usr=$F[0];
$F[5] =~ s,/[^/]+$,,;
$fs=$F[5];
%user=( "$usr" => $fs);
}
}

```
