Re: very starting help

2011-07-18 Thread Christian Walde

On Mon, 18 Jul 2011 07:34:14 +0200, anant mittal  wrote:


-w


Just a quick note: Please do not use -w in the hashbang, it forces warnings everywhere, 
even when modules didn't want warnings. You'll get weird error messages if you leave that 
in. Instead just write "use warnings;" at the top of your code and you'll be 
fine. :)

--
With regards,
Christian Walde

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: very starting help

2011-07-18 Thread Christian Walde

On Mon, 18 Jul 2011 10:19:00 +0200, Chankey Pathak  wrote:


@Christian wale: Why not to use -w for using warning everywhere?

It's good to use warnings everywhere for a good program, isn't it?


Simply put:

It activates warnings for code you did not write.

It is an extremely good idea to enable warnings for all code you write 
yourself. However if you enable it for code written by authors who did not want 
warnings, or for old modules, then you will get pointless messages you cannot 
fix.

--
With regards,
Christian Walde

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




environment variables in perl

2011-07-18 Thread Irfan Sayed
hi,

i am executing one shell script within perl script. now what i need is , i need 
to make available all the variables set by that shell script to perl script
for example 

following is the shell script
build="abc"
export build
echo $build

now the value of $build i need to access in perl script 


if i do in perl like this :
print "Environment is : $ENV{'build'}\n";

then it does not pint anything 

can someone please suggest ??

regards
irf


environment variables in perl

2011-07-18 Thread Irfan Sayed
hi,
i am executing one shell script within perl script. now what i
 need is , i need to make available all the variables set by that shell 
script to perl script
for example 

following is the shell script
build="abc"
export build
echo $build

now the value of $build i need to access in perl script 

if i do in perl like this :
print "Environment is : $ENV{'build'}\n";
then it does not pint anything 
can someone please suggest ??

regards
irf

Re: environment variables in perl

2011-07-18 Thread Shawn H Corey

On 11-07-18 07:29 AM, Irfan Sayed wrote:

if i do in perl like this :
print "Environment is : $ENV{'build'}\n";

then it does not pint anything

can someone please suggest ??


In Windows, there is only one environment.  That means if a child 
process changes it, its parent can access the change.


In Linux, each process has its own environment.  The child process 
inherits its parent's at the time of the fork and each is independent 
thereafter.


If you want two processes to communicate, you need techniques called 
Inter-Process Communication (IPC).  See `perldoc perlipc` for details.



--
Just my 0.0002 million dollars worth,
  Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software:  Fail early & often.

Eliminate software piracy:  use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Using perl to send sms through siemens tc65

2011-07-18 Thread Matevž Markovič
Hy!

First, I want to say hy to everyone! I am a fresh perl "programmer" (I
learned some things in the past few days), who is happy to get to know a new
language.
What is my problem? Actually, I did not know where to post this, so I am
just posting on this group (because I am after all a perl beginner). I work
in a company, where they want me to configure a linux centos server, that
would send and receive sms messages through the siemens tc65 terminal
siemens mc65 receiver. My first goal is to get the terminal to send a sms
message. I connected the tc65 to the computer and used minicom to check,
whether the tc65 responds to AT commands.
Well, it responded, but there is also my problem; I do not know, how to
translate that into a perl application. I thought that a module like
GSM::SMS would help me, but without any knowledge about such things, I
cannot do much at this point.

Can you please help me?

Matevž Markovič


Re: environment variables in perl

2011-07-18 Thread Christian Walde

On Mon, 18 Jul 2011 13:44:39 +0200, Shawn H Corey  wrote:


In Windows, there is only one environment.  That means if a child
process changes it, its parent can access the change.

In Linux, each process has its own environment.  The child process
inherits its parent's at the time of the fork and each is independent
thereafter.


That is most certainly not the case. On Windows %ENV behaves like it does on 
Linux:

https://gist.github.com/899a1385b703bba7f552

--
With regards,
Christian Walde

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: environment variables in perl

2011-07-18 Thread Shawn H Corey

On 11-07-18 09:24 AM, Christian Walde wrote:

On Mon, 18 Jul 2011 13:44:39 +0200, Shawn H Corey
 wrote:


In Windows, there is only one environment. That means if a child
process changes it, its parent can access the change.

In Linux, each process has its own environment. The child process
inherits its parent's at the time of the fork and each is independent
thereafter.


That is most certainly not the case. On Windows %ENV behaves like it
does on Linux:

https://gist.github.com/899a1385b703bba7f552



OK, that makes things easier.  Use `perldoc perlipc` for both.


--
Just my 0.0002 million dollars worth,
  Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software:  Fail early & often.

Eliminate software piracy:  use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Please Stop (Was: environment variables in perl)

2011-07-18 Thread Shawn H Corey
Could someone please remove lel...@claimspages.com from the mailing list 
so I won't get any more of these annoying messages?




 Original Message 
Subject: Re: Re: environment variables in perl
Date: Mon, 18 Jul 2011 09:31:05 -0400
From: RightFax E-mail Gateway
To: shawnhco...@gmail.com

Valid fax destination information could not be found
in your mail message.  The message was discarded.

Examples of properly formatted messages:

  /name=frank/fax=3217...@faxgate.company.com
  Frank Smith /name=frank/fax=3217453/ 

The original message information follows:

Received: from mx.npci.net ([10.0.1.60]) by mail.claimspages.com with
Microsoft SMTPSVC(5.0.2195.7381);
 Mon, 18 Jul 2011 09:30:33 -0400
Received: from localhost (unknown [127.0.0.1])
by mx.npci.net (Postfix) with ESMTP id 1DA252E46D7
for ; Mon, 18 Jul 2011 13:30:33 + (UTC)
X-Virus-Scanned: amavisd-new at nationwide.net, cpdev20.com, claimspages.com
Received: from mx.npci.net ([127.0.0.1])
by localhost (del-mx1.nationwide.net [127.0.0.1]) (amavisd-new, port
10024)
with ESMTP id MI6gJtK7KlS8 for ;
Mon, 18 Jul 2011 09:30:28 -0400 (EDT)
Received: from x6.develooper.com (x6.develooper.com [207.171.7.86])
by mx.npci.net (Postfix) with ESMTP id 39AAA2E4605
for ; Mon, 18 Jul 2011 09:30:28 -0400 (EDT)
Received: from lists-nntp.develooper.com (localhost.localdomain [127.0.0.1])
by x6.develooper.com (Postfix) with SMTP id 36BB117AFD
for ; Mon, 18 Jul 2011 06:30:27 -0700 (PDT)
Received: (qmail 9521 invoked by uid 514); 18 Jul 2011 13:29:50 -
Mailing-List: contact beginners-h...@perl.org; run by ezmlm
Precedence: bulk
List-Post: 
List-Help: 
List-Unsubscribe: 
List-Subscribe: 
List-Id: 
Delivered-To: mailing list beginners@perl.org
Received: (qmail 9512 invoked from network); 18 Jul 2011 13:29:50 -
Received: from x1.develooper.com (207.171.7.70)
  by x6.develooper.com with SMTP; 18 Jul 2011 13:29:50 -
Received: (qmail 26240 invoked by uid 225); 18 Jul 2011 13:29:50 -
Delivered-To: beginners@perl.org
Received: (qmail 26236 invoked by alias); 18 Jul 2011 13:29:49 -
X-Spam-Check-By: la.mx.develooper.com
Received: from mail-vw0-f41.google.com (HELO mail-vw0-f41.google.com)
(209.85.212.41)
by la.mx.develooper.com (qpsmtpd/0.28) with ESMTP; Mon, 18 Jul 2011
06:29:44 -0700
Received: by vws4 with SMTP id 4so3098673vws.14
for ; Mon, 18 Jul 2011 06:29:40 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=gamma;

h=message-id:date:from:user-agent:mime-version:to:subject:references
 :in-reply-to:content-type:content-transfer-encoding;
bh=0oU4EoHhRGGQ+TFWKrCgE8L4pIxsgHBURGWQoZr08zo=;

b=vyA9wm2NsoAHFInyWqaAcgbA/r4ZD0qNVc+GISayCEfhqPXBJp/IYx3tPpPXUlbpzx

yqJvuwmYlTsvW79qzA/sJiX2RPz/hVElxvy8Ne7lxlIre/W3XiTcIrXOgTBpabZtpDlb
 7R+qlmo6qSEIK414Q5LVeoIm4WTVqceWJTxJo=
Received: by 10.52.174.113 with SMTP id br17mr2061178vdc.107.1310995780751;
Mon, 18 Jul 2011 06:29:40 -0700 (PDT)
Received: from [192.168.2.11] (bas3-ottawa10-1279403673.dsl.bell.ca 
[76.66.38.1

53])
by mx.google.com with ESMTPS id 
pm1sm1853114vcb.33.2011.07.18.06.29.39

(version=SSLv3 cipher=OTHER);
Mon, 18 Jul 2011 06:29:39 -0700 (PDT)
Message-ID: <4e243542.9060...@gmail.com>
Date: Mon, 18 Jul 2011 09:29:38 -0400
From: Shawn H Corey 
User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.18) 
Gecko/201106

17 Thunderbird/3.1.11
MIME-Version: 1.0
To: beginners@perl.org
Subject: Re: environment variables in perl
References: <1310988574.50369.yahoomail...@web125509.mail.ne1.yahoo.com>
<4e241ca7.2080...@gmail.com> 
In-Reply-To: 
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Return-Path: beginners-return-117932-lellis=claimspages@perl.org
X-OriginalArrivalTime: 18 Jul 2011 13:30:33.0204 (UTC) 
FILETIME=[DEA54340:01CC4

54E]

On 11-07-18 09:24 AM, Christian Walde wrote:

On Mon, 18 Jul 2011 13:44:39 +0200, Shawn H Corey
 wrote:


In Windows, there is only one environment. That means if a child
process changes it, its parent can access the change.

In Linux, each process has its own environment. The child process
inherits its parent's at the time of the fork and each is independent
thereafter.


That is most certainly not the case. On Windows %ENV behaves like it
does on Linux:

https://gist.github.com/899a1385b703bba7f552



OK, that makes things easier.  Use `perldoc perlipc` for both.


--
Just my 0.0002 million dollars worth,
   Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software:  Fail early & often.

Eliminate software piracy:  use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscr...@

Re: Using perl to send sms through siemens tc65

2011-07-18 Thread Shlomi Fish
Hi Matevž ,

On Mon, 18 Jul 2011 15:21:09 +0200
Matevž Markovič  wrote:

> Hy!
> 
> First, I want to say hy to everyone! I am a fresh perl "programmer" (I
> learned some things in the past few days), who is happy to get to know a new
> language.
> What is my problem? Actually, I did not know where to post this, so I am
> just posting on this group (because I am after all a perl beginner). I work
> in a company, where they want me to configure a linux centos server, that
> would send and receive sms messages through the siemens tc65 terminal
> siemens mc65 receiver. My first goal is to get the terminal to send a sms
> message. I connected the tc65 to the computer and used minicom to check,
> whether the tc65 responds to AT commands.
> Well, it responded, but there is also my problem; I do not know, how to
> translate that into a perl application. I thought that a module like
> GSM::SMS would help me, but without any knowledge about such things, I
> cannot do much at this point.
> 
> Can you please help me?
> 

Well, if you can do it manually using minicom, you can also probably do it in
Perl using a serial port module:

http://metacpan.org/release/Device-SerialPort

I'm not familiar with GSM::SMS and have never sent SMS messages using Perl, so
I cannot help you further, but it may provide a better interface than working
with the serial port directly.

To properly learn Perl, see http://perl-begin.org/ .

Regards,

Shlomi Fish

> Matevž Markovič



-- 
-
Shlomi Fish   http://www.shlomifish.org/
Freecell Solver - http://fc-solve.berlios.de/

 sub id { my $self = shift; $json_parser_for{ $self }
->decode($json_for{ $self })->{id} } # Inside‐out JSON‐notated objects

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Using perl to send sms through siemens tc65

2011-07-18 Thread Rob Coops
On Mon, Jul 18, 2011 at 8:38 PM, Shlomi Fish  wrote:

> Hi Matevž ,
>
> On Mon, 18 Jul 2011 15:21:09 +0200
> Matevž Markovič  wrote:
>
> > Hy!
> >
> > First, I want to say hy to everyone! I am a fresh perl "programmer" (I
> > learned some things in the past few days), who is happy to get to know a
> new
> > language.
> > What is my problem? Actually, I did not know where to post this, so I am
> > just posting on this group (because I am after all a perl beginner). I
> work
> > in a company, where they want me to configure a linux centos server, that
> > would send and receive sms messages through the siemens tc65 terminal
> > siemens mc65 receiver. My first goal is to get the terminal to send a sms
> > message. I connected the tc65 to the computer and used minicom to check,
> > whether the tc65 responds to AT commands.
> > Well, it responded, but there is also my problem; I do not know, how to
> > translate that into a perl application. I thought that a module like
> > GSM::SMS would help me, but without any knowledge about such things, I
> > cannot do much at this point.
> >
> > Can you please help me?
> >
>
> Well, if you can do it manually using minicom, you can also probably do it
> in
> Perl using a serial port module:
>
> http://metacpan.org/release/Device-SerialPort
>
> I'm not familiar with GSM::SMS and have never sent SMS messages using Perl,
> so
> I cannot help you further, but it may provide a better interface than
> working
> with the serial port directly.
>
> To properly learn Perl, see http://perl-begin.org/ .
>
> Regards,
>
>Shlomi Fish
>
> > Matevž Markovič
>
>
>
> --
> -
> Shlomi Fish   http://www.shlomifish.org/
> Freecell Solver - http://fc-solve.berlios.de/
>
>  sub id { my $self = shift; $json_parser_for{ $self }
>->decode($json_for{ $self })->{id} } # Inside‐out JSON‐notated objects
>
> Please reply to list if it's a mailing list post - http://shlom.in/reply .
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>
Having had a quick look at the GSM::SMS module the intor says: *Out of the
box, it comes with a serial transport*, and a bit later: *A good start is to
read the docs for
GSM::SMS::NBS
*

To me it looks like this module will do what you want but to be honest I
have no idea if it ill I have never used it. I would suggest having a look
at the example page. It looks quite straight forward from the examples I
have seen.

Still not much help I know but the good thing is that you have found your
way to CPAN and found a module that seems to do what you want it to do. Now
the only thing left to do is just giving it a try and see what it does. In
the worst case scenario you can always do as Shlomi suggested and have a go
at the serial port interface your self but using a purpose build module will
make your life a lot easier in most cases.

Regards,

Rob Coops


File::Find script questions

2011-07-18 Thread Marc
I've written a script to traverse my web server, find any files called 
"error_log", e-mail them to me, and then delete them.  It is triggered by cron 
twice a day.

The script works great but I'd like to get advice on how I can clean up 
my code, so any comments are welcome.

Also, do I really need the foreach block in there?  I couldn't get it 
to work without it, but it seems like I should be able to. =:\

Thanks,
Marc



#!/usr/bin/perl

use strict;
use warnings;

use File::Find;
use File::HomeDir;

my $path_to_search = File::HomeDir->my_home.'/public_html';
my $file_name  = 'error_log';
my $from_address   = 'x...@xxx.com';
my $to_address = 'x...@xxx.com';
my $mail_app   = '/usr/sbin/sendmail';
my $subject= 'An "error_log" was found';

find(\&wanted, $path_to_search);

sub wanted {
if ($File::Find::name =~ /$file_name/) {
my $msg = "MIME-Version: 1.0\n".
  "Content-Type: text/plain\n".
  "To: $to_address\n".
  "From: $from_address\n".
  "Subject: $subject\n\n";

open (my $mail_fh, "|$mail_app -t -oi -oem") || die "Can't open 
sendmail!";
print {$mail_fh} "$msg";
print $msg;
open (my $file_fh, '<', $File::Find::name) || die 
"Can't open $file_name: $!";
my (@lines) = <$file_fh>;
foreach my $line (@lines) {
print "$line";
}
print {$mail_fh} "@lines";
close ($file_fh) || die "Can't close file";
close $mail_fh || die "Can't close mail_fh";
unlink ($File::Find::name);
}
}



In case anyone's interested, here's the shell script it replaces:

#!/bin/sh

## Variables ##
FILES=`find /home/user/public_html -name error_log`
ADDRESS=x...@xxx.com

for file in $FILES
  do
if [ -e "$file" ]
then
  mail -s "$file" $ADDRESS < $file
  rm -r $file  # delete log file
fi
  done


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: File::Find script questions

2011-07-18 Thread Hal Wigoda
Why clean it up when it works and is not obfusticating.

On Mon, Jul 18, 2011 at 8:59 PM, Marc  wrote:
>        I've written a script to traverse my web server, find any files called 
> "error_log", e-mail them to me, and then delete them.  It is triggered by 
> cron twice a day.
>
>        The script works great but I'd like to get advice on how I can clean 
> up my code, so any comments are welcome.
>
>        Also, do I really need the foreach block in there?  I couldn't get it 
> to work without it, but it seems like I should be able to. =:\
>
> Thanks,
> Marc
>
> 
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> use File::Find;
> use File::HomeDir;
>
> my $path_to_search = File::HomeDir->my_home.'/public_html';
> my $file_name      = 'error_log';
> my $from_address   = 'x...@xxx.com';
> my $to_address     = 'x...@xxx.com';
> my $mail_app       = '/usr/sbin/sendmail';
> my $subject        = 'An "error_log" was found';
>
> find(\&wanted, $path_to_search);
>
> sub wanted {
>        if ($File::Find::name =~ /$file_name/) {
>                my $msg = "MIME-Version: 1.0\n".
>                                  "Content-Type: text/plain\n".
>                                  "To: $to_address\n".
>                                  "From: $from_address\n".
>                                  "Subject: $subject\n\n";
>
>                open (my $mail_fh, "|$mail_app -t -oi -oem") || die "Can't 
> open sendmail!";
>                        print {$mail_fh} "$msg";
>                        print $msg;
>                        open (my $file_fh, '<', $File::Find::name) || die 
> "Can't open $file_name: $!";
>                                my (@lines) = <$file_fh>;
>                                foreach my $line (@lines) {
>                                        print "$line";
>                                }
>                                print {$mail_fh} "@lines";
>                        close ($file_fh) || die "Can't close file";
>                close $mail_fh || die "Can't close mail_fh";
>                unlink ($File::Find::name);
>        }
> }
>
> 
>
> In case anyone's interested, here's the shell script it replaces:
>
> #!/bin/sh
>
> ## Variables ##
> FILES=`find /home/user/public_html -name error_log`
> ADDRESS=x...@xxx.com
>
> for file in $FILES
>  do
>    if [ -e "$file" ]
>    then
>      mail -s "$file" $ADDRESS < $file
>      rm -r $file  # delete log file
>    fi
>  done
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>



-- 
-
Chicago
Hal Wigoda

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: File::Find script questions

2011-07-18 Thread John W. Krahn

Marc wrote:

I've written a script to traverse my web server, find any files called
"error_log", e-mail them to me, and then delete them.  It is triggered
by cron twice a day.

The script works great but I'd like to get advice on how I can clean
up my code, so any comments are welcome.

Also, do I really need the foreach block in there?  I couldn't get it
to work without it, but it seems like I should be able to. =:\

Thanks,
Marc



#!/usr/bin/perl

use strict;
use warnings;

use File::Find;
use File::HomeDir;

my $path_to_search = File::HomeDir->my_home.'/public_html';


The use of File::HomeDir is great, if this program were running on a 
variety of different operating systems, but your use of 
'/usr/sbin/sendmail' implies that this is only running on a Unix-like 
operating system so you probably only need $ENV{HOME}.




my $file_name  = 'error_log';
my $from_address   = 'x...@xxx.com';
my $to_address = 'x...@xxx.com';
my $mail_app   = '/usr/sbin/sendmail';
my $subject= 'An "error_log" was found';

find(\&wanted, $path_to_search);

sub wanted {
if ($File::Find::name =~ /$file_name/) {


Why compare the full path using a regular expression?  Just compare the 
file name itself:


return if $_ ne $file_name;



my $msg = "MIME-Version: 1.0\n".
  "Content-Type: text/plain\n".
  "To: $to_address\n".
  "From: $from_address\n".
  "Subject: $subject\n\n";


This variable doesn't change during inside the loop, so don't put it 
inside the loop.




open (my $mail_fh, "|$mail_app -t -oi -oem") || die "Can't open 
sendmail!";


You should include the $! variable in the error message so you know why 
it failed.  You should use the list form of pipe open so you don't 
invoke a shell to run $mail_app:


		open my $mail_fh, '|-', $mail_app, '-t', '-oi' '-oem' or die "Can't 
open '$mail_app' because: $!";




print {$mail_fh} "$msg";


No need to copy $msg to a string:

print $mail_fh $msg;



print $msg;
open (my $file_fh, '<', $File::Find::name) || die "Can't 
open $file_name: $!";
my (@lines) =<$file_fh>;
foreach my $line (@lines) {
print "$line";
}
print {$mail_fh} "@lines";


No, you don't need a foreach loop.  And no, you don't need to quote 
variables (copy variable contents to strings.)


my @lines = <$file_fh>;
print @lines;
print $mail_fh @lines;



close ($file_fh) || die "Can't close file";


You should include the $! variable in the error message so you know why 
it failed.




close $mail_fh || die "Can't close mail_fh";


You should verify that the pipe was closed correctly:

close $mail_fh or warn $! ? "Error closing '$mail_app' pipe: $!"
  : "Exit status $? from '$mail_app'";



unlink ($File::Find::name);


You should verify that unlink() worked correctly:

		unlink( $File::Find::name ) or warn "Cannot unlink '$File::Find::name' 
because: $!";




}
}



In case anyone's interested, here's the shell script it replaces:

#!/bin/sh

## Variables ##
FILES=`find /home/user/public_html -name error_log`
ADDRESS=x...@xxx.com

for file in $FILES
   do
 if [ -e "$file" ]
 then
   mail -s "$file" $ADDRESS<  $file
   rm -r $file  # delete log file
 fi
   done




John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: File::Find script questions

2011-07-18 Thread Uri Guttman
> "HW" == Hal Wigoda  writes:

  HW> Why clean it up when it works and is not obfusticating.

because he asked for comments and it will be educational to all on the
list.

uri

-- 
Uri Guttman  --  uri AT perlhunter DOT com  ---  http://www.perlhunter.com --
  Perl Developer Recruiting and Placement Services  -
-  Perl Code Review, Architecture, Development, Training, Support ---

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: File::Find script questions

2011-07-18 Thread Uri Guttman
> "M" == Marc   writes:

  M>The script works great but I'd like to get advice on how I can
  M>clean up my code, so any comments are welcome.

  M>Also, do I really need the foreach block in there?  I couldn't
  M>get it to work without it, but it seems like I should be able
  M>to. =:\

  M> use strict;
  M> use warnings;

good.

  M> use File::Find;
  M> use File::HomeDir;

  M> find(\&wanted, $path_to_search);

  M> sub wanted {
  M>if ($File::Find::name =~ /$file_name/) {

it would be much cleaner IMO to collect the paths of the files you want
and then processs those paths in a sub. the rule is generally collect
the data you need and process later and elsewhere. this is good for
several reasons. you could write each part to be more generic and
reusable. when they are tied like this you have only one use for the
code. also it make it easier to debug. you can test the mailer sub by
passing it a path and never executing the search part.

  M>my $msg = "MIME-Version: 1.0\n".
  M>  "Content-Type: text/plain\n".
  M>  "To: $to_address\n".
  M>  "From: $from_address\n".
  M>  "Subject: $subject\n\n";

instead of multiple lines with . operators this is much easier with a
here doc. no quotes, no \n, no . ops.

my $msg = print {$mail_fh} "$msg";

don't quote scalar vars like that. it makes an unneeded copy and can be
a bug in some situations.

also you can get the file text and do one print to the mailer.

  M>print $msg;
  M>open (my $file_fh, '<', $File::Find::name) ||
  M>die "Can't open $file_name: $!";

have to plug File::Slurp here. it can read the whole file into a scalar
and is much cleaner and faster than your code.


  M>my (@lines) = <$file_fh>;
  M>foreach my $line (@lines) {
  M>print "$line";
  M>}
  M>print {$mail_fh} "@lines";

that quoted array will put a space between each line. it may not be what
you want. this should be all you need:

use File::Slurp ;
my $log_text = read_file( $File::Find::name ) ;

print $mail_fh $msg, $log_text ;
print $log_text ;

  M>close ($file_fh) || die "Can't close file";

not needed with slurp.

  M>close $mail_fh || die "Can't close mail_fh";
  M>unlink ($File::Find::name);
  M>}
  M> }


  M> 

  M> In case anyone's interested, here's the shell script it replaces:

if you simplified it to separate subs like i mention and use mailer
modules and file::slurp, it will be almost as short as the shell version.

uri

-- 
Uri Guttman  --  uri AT perlhunter DOT com  ---  http://www.perlhunter.com --
  Perl Developer Recruiting and Placement Services  -
-  Perl Code Review, Architecture, Development, Training, Support ---

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Using perl to send sms through siemens tc65

2011-07-18 Thread Matevž Markovič
Dear Mr. Coops and Fish,

Thank you for your advices. I will first try the modules,, and if it does
not work, then I will take the "manual" way. Should be fun :).

Thank you,

Matevž