Re: newbie question for parsing incoming mails

2017-02-16 Thread Jim Gibson

> On Feb 15, 2017, at 9:56 PM, Eko Budiharto  wrote:
> 
> Jim,
> I have one a couple more questions.
> -. For the header, what if, I just need the subject, the from, and the 
> recipient, what is the command? I read the manual in the 
> https://metacpan.org/pod/Email::MIME#header, it does not tell me how to 
> extract all of those.
> -. And then, for the email contents of the body, it just shows Content-Type: 
> multipart/alternative; boundary=f403045de9521d20cc054874ce1a 
> instead of the contents of the email body. How to extract it into string 
> because it is under html format.

Please post all questions to the list. There are other people who can answer 
your questions better than I can.

I just parse the text of the email using regular expressions, line by line:

my @lines = split(/[\n\r]+/,$text);

for( my $i = 0; $i <= $#lines; $i++ ) {
my $line = $lines[$i];
print "$i. $line\n" if $xdebug;

if( $line =~ m{ \A Subject:\ (.*) \z }x ) {
$subject = $1;

etc.

You can also try to use the Email::MIME::header method:

my $from = $mime->header(‘From’):

I haven’t used that, but it might be worth trying. Email::MIME is an extension 
of the Email::Simple class, so you can look at the documentation for that 
module as well. 
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: newbie question for parsing incoming mails

2017-02-15 Thread Eko Budiharto
dear all,
I have one a couple questions.
-. For the header, what if, I just need the subject, the from, and the 
recipient, what is the command? I read the manual in the 
https://metacpan.org/pod/Email::MIME#header 
, it does not tell me how to 
extract all of those.
-. And then, for the email contents of the body, it just shows Content-Type: 
multipart/alternative; boundary=f403045de9521d20cc054874ce1a 
instead of the contents of the email body. How to extract it into string 
because it is under html format.

> On Feb 16, 2017, at 11:45, Jim Gibson  wrote:
> 
> 
>> On Feb 15, 2017, at 8:10 PM, Eko Budiharto  wrote:
>> 
>> dear Jim,
>> I tried to add lines to read file like this:
>> 
>> use Email::MIME;
>> 
>> my $file = '/var/qmail/mailnames/> name>/support/Maildir/cur/1487041394.M984019P23084V0803I00E03878.ABCD.NET,S=3987:2,';
>> open my $ifh, '<', $file
>>  or die "Cannot open '$file' for reading: $!";
>> local $/ = '';
>> my $contents = <$ifh>;
>> close( $ifh );
>> 
>> my $mime = Email::MIME->($contents);  —> line 13
>> 
>> my @parts = $mime->parts();
>> 
>> for my $npart ( 0..$#parts ) {
>>  my $part = $parts[$npart];
>>  my $header = $part->header_obj();
>>  my $htext = $header->as_string();
>>  my $body = $part->body();
>> 
>>  print $header;
>> }
>> 
>> after I ran its from CLI, I got an error message Undefined subroutine 
>> ::MIME called at line 13.
>> 
>> what does the error mean?
> 
> That means I mistyped the line. It should be this:
> 
>   my $mime = Email::MIME->new($contents);
> 
> 



Re: newbie question for parsing incoming mails

2017-02-15 Thread Jim Gibson
On Feb 15, 2017, at 7:10 PM, Eko Budiharto  wrote:
> 
> Jim, 
> if I want to extract all incoming emails from my qmail emails, how can 
> specify the folder location and specify the file name since the file name 
> always different?
> 
> Thx.

Use File::Find or opendir and readdir to find all of the emails in a directory 
tree. You need to figure out the parent directory, if there is one, of the 
local storage for your email client.

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




Re: newbie question for parsing incoming mails

2017-02-15 Thread Jim Gibson

> On Feb 15, 2017, at 8:10 PM, Eko Budiharto  wrote:
> 
> dear Jim,
> I tried to add lines to read file like this:
> 
> use Email::MIME;
> 
> my $file = '/var/qmail/mailnames/ name>/support/Maildir/cur/1487041394.M984019P23084V0803I00E03878.ABCD.NET,S=3987:2,';
> open my $ifh, '<', $file
>   or die "Cannot open '$file' for reading: $!";
> local $/ = '';
> my $contents = <$ifh>;
> close( $ifh );
> 
> my $mime = Email::MIME->($contents);  —> line 13
> 
> my @parts = $mime->parts();
> 
> for my $npart ( 0..$#parts ) {
>   my $part = $parts[$npart];
>   my $header = $part->header_obj();
>   my $htext = $header->as_string();
>   my $body = $part->body();
> 
>   print $header;
> }
> 
> after I ran its from CLI, I got an error message Undefined subroutine 
> ::MIME called at line 13.
> 
> what does the error mean?

That means I mistyped the line. It should be this:

my $mime = Email::MIME->new($contents);

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




Re: newbie question for parsing incoming mails

2017-02-15 Thread Eko Budiharto
dear Jim,
I tried to add lines to read file like this:

use Email::MIME;

my $file = '/var/qmail/mailnames//support/Maildir/cur/1487041394.M984019P23084V0803I00E03878.ABCD.NET,S=3987:2,';
open my $ifh, '<', $file
  or die "Cannot open '$file' for reading: $!";
local $/ = '';
my $contents = <$ifh>;
close( $ifh );

my $mime = Email::MIME->($contents);  —> line 13

my @parts = $mime->parts();

for my $npart ( 0..$#parts ) {
  my $part = $parts[$npart];
  my $header = $part->header_obj();
  my $htext = $header->as_string();
  my $body = $part->body();

  print $header;
}

after I ran its from CLI, I got an error message Undefined subroutine 
::MIME called at line 13.

what does the error mean?



> On Feb 15, 2017, at 22:56, Jim Gibson  wrote:
> 
> I use Email::MIME to parse email messages. After reading the email file into 
> the variable $text I do this:
> 
> my $mime = Email::MIME->($text);
> my @parts = $mime->parts();
> 
> for my $npart ( 0..$#parts ) {
>  my $part = $parts[$npart];
>  my $header = $part->header_obj();
>  my $htext = $header->as_string();
>  my $body = $part->body();
>  …
> }
> 
> 
> 
> 
> 
> Jim Gibson



Re: newbie question for parsing incoming mails

2017-02-15 Thread Eko Budiharto
Jim, 
if I want to extract all incoming emails from my qmail emails, how can specify 
the folder location and specify the file name since the file name always 
different?

Thx.
> On Feb 15, 2017, at 22:56, Jim Gibson  wrote:
> 
>> 
>> On Feb 14, 2017, at 10:38 PM, Eko Budiharto  wrote:
>> 
>> dear all,
>> I have a question. 
>> If I would like to parse all incoming mails from my qmail, which perl module 
>> is easy to use?
>> my qmail emails incoming is /var/qmail/mailnames//support. In 
>> this folder I already have preline in .qmail for piping emails to my perl 
>> script.
>> And, my perl script what I already have is like this:
>> 
>> #!/usr/bin/perl -w
>> 
>> use Mail::Internet;
>> 
>> my $mail = Mail::Internet->new( [  ] );
>> my $headers = $mail->head->header_hashref;
>> my @subject = @{${$headers}{'Subject'}}; # or otherwsie do it in list 
>> context it works
>> 
>> print @subject;
>> 
>> 
>> when I run it, I do not get anything.
>> 
>> please help.
> 
> I use Email::MIME to parse email messages. After reading the email file into 
> the variable $text I do this:
> 
> my $mime = Email::MIME->($text);
> my @parts = $mime->parts();
> 
> for my $npart ( 0..$#parts ) {
>  my $part = $parts[$npart];
>  my $header = $part->header_obj();
>  my $htext = $header->as_string();
>  my $body = $part->body();
>  …
> }
> 
> 
> 
> 
> 
> Jim Gibson



Re: newbie question for parsing incoming mails

2017-02-15 Thread Andy Bach
> when I run it, I do not get anything.
Hmm, how are you testing it. I put a single header and msg in /tmp/ml.txt
(Subject: training.error Trouble wiwb) and:
$ parse_email_simple.pl < /tmp/ml.txt
training.error Trouble wiwb (8 lines)
$ cat /tmp/ml.txt | parse_email_simple.pl
training.error Trouble wiwb (8 lines)

In addition, if I take out STDOUT
my $mail = Mail::Internet->new( [ <> ] );

I can do:
$ parse_email_simple.pl  /tmp/ml.txt

as Perl'll open the command line file name argument

Note, these'll treat all input as a single msg.

On Wed, Feb 15, 2017 at 12:38 AM, Eko Budiharto 
wrote:

> dear all,
> I have a question.
> If I would like to parse all incoming mails from my qmail, which perl
> module is easy to use?
> my qmail emails incoming is /var/qmail/mailnames//support.
> In this folder I already have preline in .qmail for piping emails to my
> perl script.
> And, my perl script what I already have is like this:
>
> #!/usr/bin/perl -w
>
> use Mail::Internet;
>
> my $mail = Mail::Internet->new( [  ] );
> my $headers = $mail->head->header_hashref;
> my @subject = @{${$headers}{'Subject'}}; # or otherwsie do it in list
> context it works
>
> print @subject;
>
>
> when I run it, I do not get anything.
>
> please help.
>
> regards,
> Eko
>



-- 

a

Andy Bach,
afb...@gmail.com
608 658-1890 cell
608 261-5738 wk


Re: newbie question for parsing incoming mails

2017-02-15 Thread Jim Gibson

> On Feb 14, 2017, at 10:38 PM, Eko Budiharto  wrote:
> 
> dear all,
> I have a question. 
> If I would like to parse all incoming mails from my qmail, which perl module 
> is easy to use?
> my qmail emails incoming is /var/qmail/mailnames//support. In 
> this folder I already have preline in .qmail for piping emails to my perl 
> script.
> And, my perl script what I already have is like this:
>  
> #!/usr/bin/perl -w
>  
> use Mail::Internet;
> 
> my $mail = Mail::Internet->new( [  ] );
> my $headers = $mail->head->header_hashref;
> my @subject = @{${$headers}{'Subject'}}; # or otherwsie do it in list context 
> it works
> 
> print @subject;
> 
> 
> when I run it, I do not get anything.
> 
> please help.

I use Email::MIME to parse email messages. After reading the email file into 
the variable $text I do this:

my $mime = Email::MIME->($text);
my @parts = $mime->parts();

for my $npart ( 0..$#parts ) {
  my $part = $parts[$npart];
  my $header = $part->header_obj();
  my $htext = $header->as_string();
  my $body = $part->body();
  …
}





Jim Gibson

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




Re: newbie pipe stdin to sendmail

2016-11-03 Thread Mark Coetser


On 02/11/2016 22:14, Andy Bach wrote:

#!/usr/bin/perl

my $vacation_forward = 'vacat...@domain.com ';

open(OUT, "|/usr/sbin/sendmail $vacation_forward") or die ("Can't
sendmail - $!");
my $email_addr_to = '';
while ( <>) {
 $email_addr_to = $1 if /^To: (.*)$/;
 print OUT $_;
 }
close (OUT);

if ($email_addr_to) {
 print "$email_addr_to\n";
 }


Thank you

Nice and simple and thanks for the explanation it helps me understand.

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




Re: newbie pipe stdin to sendmail

2016-11-02 Thread Mark Coetser

On 02/11/2016 19:35, Andy Bach wrote:


On Wed, Nov 2, 2016 at 9:13 AM, Mark Coetser > wrote:


$parser->ignore_errors(1);
$parser->output_to_core(1);

my $entity = $parser->parse(\*STDIN);
my $error = ($@ || $parser->last_error);

#get email headers


...
my ($header, $body);

{ local $/ = "";
 $header = ;
 undef $/;
$body = ;
}

Seems like you've already consumed STDIN in the parser, so there's
nothing left in your local block to copy.  My guess is you want to print
$entity to MAIL.  Note, you should always use a conditional on open
stmts, often
open(my $mh, "|$sendmail $vac) or die "can't exec $sendmail: $!";

Using a lexical, instead of a NAME is modern perl, you still do
print $mh "header: ...


maybe (perldoc MIME::Parser):
 ### Congratulations: you now have a (possibly multipart) MIME
entity!
   $entity->dump_skeleton;  # for debugging


Again thanks for the help, to simply things how can I read stdin into a 
variable that I could parse twice. I tried to simplfy things as follows 
but get the same result..


#!/usr/bin/perl

my $vacation_forward = "vacation\@domain.com";

open(OUT, "|/usr/sbin/sendmail $vacation_forward") or die ("Can't 
sendmail - $!");

while (my $line = ) {
$email_addr_to = $1 if $line =~ /^To: (.*)$/;
print OUT;
}
close (OUT);

if ($email_addr_to) {
print "$email_addr_to\n";
}




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




Re: Newbie creating Perl web report

2014-02-07 Thread Octavian Rasnita
CGI is just an interface between a program and a web server.
Other newer interfaces are fastcgi, fcgid, mod_perl, psgi...

Using another interface than CGI has some advantages and some disadvantages.
The main advantage is that the programs are persistent, so they are ran once, 
usually when the web server starts, then they respond very fast to HTTP 
requests.
The disadvantage is that they are a little more complicated than just using 
CGI, plus that they consume some more memory.

If you need to create a public web site that should always respond as fast as 
possible, it is definitely required to use the modern interfaces, preferably 
psgi.
If you need to create very small scripts just for internal use, with no 
relations among them, so no central authentication/authorization for example, 
and if they are not accessed by very many users in a day and it is OK if the 
users wait 2 more seconds to open the page, then you can use CGI with no 
problem.
The CGI scripts are not persistent, so they are loaded/compiled/executed each 
time they are accessed with an HTTP request. If you change something in a CGI 
script, you don't need to stop/start it after you do the change.

Using web frameworks is another story.
If you need to do very very simple and dirty scripts that do very simple things 
and return a very simple web interface, then you can create them without using 
higher level modules like an ORM, a templating engine, a form manager.

If the programs are a little bigger, and if they need to be easily maintainable 
or if the web interfaces they need to create are a little more complex, then it 
is better to use that kind of module, and separate the Perl code and the HTML, 
Javascript, CSS code in distinct files.

If you need to use those modules and you actually want to create a web 
application, then it is easier if you use a web framework. The most used Perl 
web frameworks these days are Catalyst, Dancer and Mojolicious.
They help you to organize the application in a way that makes it easy to 
maintain on long term.

You can use the web frameworks with any interface you like (if the framework 
supports it), for example you can use a Catalyst - based web app with the CGI 
interface, but because CGI interface needs to load the entire application on 
each request, and when using a web framework it usually needs to load a lot of 
module, it works very very slow.
So usually when the application uses a web framework it also uses a persistent 
interface like psgi or fastcgi or mod_perl.

If the scripts are accessed only localy, if they are complex enough and need to 
be easy maintainable and you want to use a web framework, you need to choose 
among Catalyst, Dancer, Mojolicious, CGI::Application or other Perl framework.

For smaller projects that require very few features, Dancer and Mojolicious 
Lite may be better than Catalyst because they are easier to learn, but for 
bigger applications or applications that may become bigger Catalyst is better.

Also, if you know to use some Perl modules like an ORM like DBIx::Class, a 
templating system like Template-Toolkit or others, Catalyst is better because 
it is very flexible and lets you use what you want.
But if you don't know anything, Mojolicious may be better because it does the 
things in its own way. It also allows you to use other Perl modules, but the 
recommendations are always to use its standard way.

So there is more than one way and you can choose depending on your needs.

--Octavian

  - Original Message - 
  From: Robert Freiberger 
  To: begin Perl Beginners 
  Sent: Friday, February 07, 2014 8:30 AM
  Subject: Newbie creating Perl web report


  Hello everyone, 


  I'm somewhat confused on a topic of CGI vs a web frame work for creating 
simple applications. I've been reading Beginning Perl by Ovid and seems like 
it's recommend to use a frame work instead of the older method of CGI module. 


  Is frame works like Mojolicious or Flask the new recommended method for 
creating web based tools or scripts? My goal is making some reports for work 
that would stay internally but also apply what I learn to other external side 
projects. 


  Thanks,
  Robert



  -- 
  Robert Freiberger



Re: Newbie creating Perl web report

2014-02-07 Thread Chankey Pathak
Very good answer Octavian Rasnita :)


On Fri, Feb 7, 2014 at 2:00 PM, Octavian Rasnita orasn...@gmail.com wrote:

  CGI is just an interface between a program and a web server.
 Other newer interfaces are fastcgi, fcgid, mod_perl, psgi...

 Using another interface than CGI has some advantages and some
 disadvantages.
 The main advantage is that the programs are persistent, so they are ran
 once, usually when the web server starts, then they respond very fast to
 HTTP requests.
 The disadvantage is that they are a little more complicated than just
 using CGI, plus that they consume some more memory.

 If you need to create a public web site that should always respond as fast
 as possible, it is definitely required to use the modern interfaces,
 preferably psgi.
 If you need to create very small scripts just for internal use, with no
 relations among them, so no central authentication/authorization for
 example, and if they are not accessed by very many users in a day and it is
 OK if the users wait 2 more seconds to open the page, then you can use CGI
 with no problem.
 The CGI scripts are not persistent, so they are loaded/compiled/executed
 each time they are accessed with an HTTP request. If you change something
 in a CGI script, you don't need to stop/start it after you do the change.

 Using web frameworks is another story.
 If you need to do very very simple and dirty scripts that do very simple
 things and return a very simple web interface, then you can create them
 without using higher level modules like an ORM, a templating engine, a form
 manager.

 If the programs are a little bigger, and if they need to be easily
 maintainable or if the web interfaces they need to create are a little more
 complex, then it is better to use that kind of module, and separate the
 Perl code and the HTML, Javascript, CSS code in distinct files.

 If you need to use those modules and you actually want to create a web
 application, then it is easier if you use a web framework. The most used
 Perl web frameworks these days are Catalyst, Dancer and Mojolicious.
 They help you to organize the application in a way that makes it easy to
 maintain on long term.

 You can use the web frameworks with any interface you like (if the
 framework supports it), for example you can use a Catalyst - based web app
 with the CGI interface, but because CGI interface needs to load the entire
 application on each request, and when using a web framework it usually
 needs to load a lot of module, it works very very slow.
 So usually when the application uses a web framework it also uses a
 persistent interface like psgi or fastcgi or mod_perl.

 If the scripts are accessed only localy, if they are complex enough and
 need to be easy maintainable and you want to use a web framework, you need
 to choose among Catalyst, Dancer, Mojolicious, CGI::Application or other
 Perl framework.

 For smaller projects that require very few features, Dancer and
 Mojolicious Lite may be better than Catalyst because they are easier to
 learn, but for bigger applications or applications that may become bigger
 Catalyst is better.

 Also, if you know to use some Perl modules like an ORM like DBIx::Class, a
 templating system like Template-Toolkit or others, Catalyst is better
 because it is very flexible and lets you use what you want.
 But if you don't know anything, Mojolicious may be better because it does
 the things in its own way. It also allows you to use other Perl modules,
 but the recommendations are always to use its standard way.

 So there is more than one way and you can choose depending on your needs.

 --Octavian

 - Original Message -
 *From:* Robert Freiberger rfreiber...@gmail.com
 *To:* begin Perl Beginners beginners@perl.org
 *Sent:* Friday, February 07, 2014 8:30 AM
 *Subject:* Newbie creating Perl web report

 Hello everyone,

 I'm somewhat confused on a topic of CGI vs a web frame work for creating
 simple applications. I've been reading Beginning Perl by Ovid and seems
 like it's recommend to use a frame work instead of the older method of CGI
 module.

 Is frame works like Mojolicious or Flask the new recommended method for
 creating web based tools or scripts? My goal is making some reports for
 work that would stay internally but also apply what I learn to other
 external side projects.

 Thanks,
 Robert

 --
 Robert Freiberger




-- 
Regards,
Chankey Pathak http://www.linuxstall.com


Re: Newbie creating Perl web report

2014-02-07 Thread Robert Freiberger
Thank you Octavian,

That was an very clear understanding of CGI vs web frame works. I'm still
new to the Perl world and will be reading more into the modules you have
mentioned. Also greatly welcome any suggestions for tutorials on the
subject!

Thanks!


On Fri, Feb 7, 2014 at 12:45 AM, Chankey Pathak chankey...@gmail.comwrote:

 Very good answer Octavian Rasnita :)


 On Fri, Feb 7, 2014 at 2:00 PM, Octavian Rasnita orasn...@gmail.comwrote:

  CGI is just an interface between a program and a web server.
 Other newer interfaces are fastcgi, fcgid, mod_perl, psgi...

 Using another interface than CGI has some advantages and some
 disadvantages.
 The main advantage is that the programs are persistent, so they are ran
 once, usually when the web server starts, then they respond very fast to
 HTTP requests.
 The disadvantage is that they are a little more complicated than just
 using CGI, plus that they consume some more memory.

 If you need to create a public web site that should always respond as
 fast as possible, it is definitely required to use the modern interfaces,
 preferably psgi.
 If you need to create very small scripts just for internal use, with no
 relations among them, so no central authentication/authorization for
 example, and if they are not accessed by very many users in a day and it is
 OK if the users wait 2 more seconds to open the page, then you can use CGI
 with no problem.
 The CGI scripts are not persistent, so they are loaded/compiled/executed
 each time they are accessed with an HTTP request. If you change something
 in a CGI script, you don't need to stop/start it after you do the change.

 Using web frameworks is another story.
 If you need to do very very simple and dirty scripts that do very simple
 things and return a very simple web interface, then you can create them
 without using higher level modules like an ORM, a templating engine, a form
 manager.

 If the programs are a little bigger, and if they need to be easily
 maintainable or if the web interfaces they need to create are a little more
 complex, then it is better to use that kind of module, and separate the
 Perl code and the HTML, Javascript, CSS code in distinct files.

 If you need to use those modules and you actually want to create a web
 application, then it is easier if you use a web framework. The most used
 Perl web frameworks these days are Catalyst, Dancer and Mojolicious.
 They help you to organize the application in a way that makes it easy to
 maintain on long term.

 You can use the web frameworks with any interface you like (if the
 framework supports it), for example you can use a Catalyst - based web app
 with the CGI interface, but because CGI interface needs to load the entire
 application on each request, and when using a web framework it usually
 needs to load a lot of module, it works very very slow.
 So usually when the application uses a web framework it also uses a
 persistent interface like psgi or fastcgi or mod_perl.

 If the scripts are accessed only localy, if they are complex enough and
 need to be easy maintainable and you want to use a web framework, you need
 to choose among Catalyst, Dancer, Mojolicious, CGI::Application or other
 Perl framework.

 For smaller projects that require very few features, Dancer and
 Mojolicious Lite may be better than Catalyst because they are easier to
 learn, but for bigger applications or applications that may become bigger
 Catalyst is better.

 Also, if you know to use some Perl modules like an ORM like DBIx::Class,
 a templating system like Template-Toolkit or others, Catalyst is better
 because it is very flexible and lets you use what you want.
 But if you don't know anything, Mojolicious may be better because it does
 the things in its own way. It also allows you to use other Perl modules,
 but the recommendations are always to use its standard way.

 So there is more than one way and you can choose depending on your needs.

 --Octavian

 - Original Message -
 *From:* Robert Freiberger rfreiber...@gmail.com
 *To:* begin Perl Beginners beginners@perl.org
 *Sent:* Friday, February 07, 2014 8:30 AM
 *Subject:* Newbie creating Perl web report

 Hello everyone,

 I'm somewhat confused on a topic of CGI vs a web frame work for creating
 simple applications. I've been reading Beginning Perl by Ovid and seems
 like it's recommend to use a frame work instead of the older method of CGI
 module.

 Is frame works like Mojolicious or Flask the new recommended method for
 creating web based tools or scripts? My goal is making some reports for
 work that would stay internally but also apply what I learn to other
 external side projects.

 Thanks,
 Robert

 --
 Robert Freiberger




 --
 Regards,
 Chankey Pathak http://www.linuxstall.com




-- 
Robert Freiberger
510-936-1210


Re: Newbie Where to start perhaps a hash-table is what I need to learn about ?

2012-07-05 Thread Phil Pinkerton

On Jul 4, 2012, at 6:02 PM, David Precious wrote:

 On Wed, 4 Jul 2012 17:01:35 -0400
 Phil Pinkerton pcpinker...@gmail.com wrote:
 
 Very nice to know about CPAN.
 
 IMO, CPAN is one of Perl's strongest features :)
 
 
 got a syntax error though with your script
 
 syntax error at ./getACLinfo.pl line 51, near say join
 Execution of ./getACLinfo.pl aborted due to compilation errors.
 
 Whoops - as Chris pointed out, 'say' requires perl 5.10 or newer; I
 should have included 'use 5.010' in the script to indicate that, but
 forgot to (I have $PERL5OPT set to '-M5.010' on most boxes).
 
 Chris's mail contains full instructions to work round that problem.
 

ok using the example input file I got errors

Use of uninitialized value $current_resource in string eq at 
/Library/Perl/5.12/SVN/Access.pm line 70, ACL line 3.
Use of uninitialized value $current_resource in string eq at 
/Library/Perl/5.12/SVN/Access.pm line 73, ACL line 3.
Use of uninitialized value $resource_name in string eq at 
/Library/Perl/5.12/SVN/Access.pm line 259, ACL line 3.
Use of uninitialized value $resource_name in pattern match (m//) at 
/Library/Perl/5.12/SVN/Access.pm line 265, ACL line 3.
Use of uninitialized value $resource_name in concatenation (.) or string at 
/Library/Perl/5.12/SVN/Access.pm line 266, ACL line 3.
Invalid resource format in ! (format 'repo:/path')!

but substituting a copy of our actual svn access file I get

Can't add new group cvs_cscda_owners: group already exists!

So I may have a format issue with the example inout file

phil


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




Re: Newbie Where to start perhaps a hash-table is what I need to learn about ?

2012-07-05 Thread Phil Pinkerton

On Jul 4, 2012, at 5:46 PM, Chris Charley wrote:

 
 
 Phil Pinkerton  wrote in message 
 news:7a962da1-a5fb-4046-bbf5-f888dd715...@gmail.com...
 Very nice to know about CPAN.
 
 got a syntax error though with your script
 
 syntax error at ./getACLinfo.pl line 51, near say join
 Execution of ./getACLinfo.pl aborted due to compilation errors.
 
 [code]
 #!/usr/bin/perl
 
 use strict;
 use SVN::Access;
 my $acl = SVN::Access-new( acl_file = 'data' );
 
 # Walk through the resources in the ACL file:
 for my $repopath ($acl-resources) {
  # For this resource, find out what authorisations exist:
  for my $auth ($repopath-authorized) {
 
  # $repopath-authorized gives us hashrefs of group = perms
  # TODO: I don't know if it might also give individual users, if
  # had per-user grants.
  my ($group, $perms) = each %$auth;
 
  if ($group =~ s/^@//) {
  # For each group, list out the members, along with the repo
  # permissions
 
  for my $user ($acl-group($group)-members) {
  say join '~', $user, $repopath-name, $group, $perms;
  }
  } else {
  die Can't handle individual user permissions!;
  }
  }
 }
 [/code]
 
 Hello Phil,
 
 Your script needs to use 5.010 or better to use the 'say' function.
 
 Right after the 'use strict;' line, enter 'use 5.010;' or
 a newer version if that is what you have.
 
 Or, replace say with 'print':
 
 print join('~', $user, $repopath-name, $group, $perms), \n;
 
 Chris

After switching input files It partially works but only for the first 
repository in the access file?





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




Re: Newbie Where to start perhaps a hash-table is what I need to learn about ?

2012-07-05 Thread David Precious
On Thu, 5 Jul 2012 09:30:01 -0400
Phil Pinkerton pcpinker...@gmail.com wrote:
 ok using the example input file I got errors
 
 Use of uninitialized value $current_resource in string eq
 at /Library/Perl/5.12/SVN/Access.pm line 70, ACL line 3.
[...]
 Invalid resource format in ! (format 'repo:/path')!

Ah, I recall now I had to uncomment the #[groups] line in the example
input you originally supplied before the script would work.


 but substituting a copy of our actual svn access file I get
 
 Can't add new group cvs_cscda_owners: group already exists!
 
 So I may have a format issue with the example inout file

Yeah, sounds like there's something potentially wrong with the input
file - have you perhaps defined the cvs_cscda_owners group twice?



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




Re: Newbie Where to start perhaps a hash-table is what I need to learn about ?

2012-07-05 Thread Phil Pinkerton

On Jul 5, 2012, at 10:46 AM, David Precious wrote:

 On Thu, 5 Jul 2012 09:30:01 -0400
 Phil Pinkerton pcpinker...@gmail.com wrote:
 ok using the example input file I got errors
 
 Use of uninitialized value $current_resource in string eq
 at /Library/Perl/5.12/SVN/Access.pm line 70, ACL line 3.
 [...]
 Invalid resource format in ! (format 'repo:/path')!
 
 Ah, I recall now I had to uncomment the #[groups] line in the example
 input you originally supplied before the script would work.
 
 
 but substituting a copy of our actual svn access file I get
 
 Can't add new group cvs_cscda_owners: group already exists!
 
 So I may have a format issue with the example inout file
 
 Yeah, sounds like there's something potentially wrong with the input
 file - have you perhaps defined the cvs_cscda_owners group twice?
 

Yes actually there were several duplicates. Once they were all removed the 
script seemed to work but just for the first Repository entry ?

Not sure what is missing since it found all those duplicates but only seemed to 
actually complete just the first repo

The final message after listing the last entry of the first repository:

Can't handle individual user permissions! at line 60  of the script ( which 
is just that message )

die  Can't handle individual user permissions!;


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




Re: Newbie Where to start perhaps a hash-table is what I need to learn about ?

2012-07-05 Thread Phil Pinkerton

On Jul 5, 2012, at 10:46 AM, David Precious wrote:

 On Thu, 5 Jul 2012 09:30:01 -0400
 Phil Pinkerton pcpinker...@gmail.com wrote:
 ok using the example input file I got errors
 
 Use of uninitialized value $current_resource in string eq
 at /Library/Perl/5.12/SVN/Access.pm line 70, ACL line 3.
 [...]
 Invalid resource format in ! (format 'repo:/path')!
 
 Ah, I recall now I had to uncomment the #[groups] line in the example
 input you originally supplied before the script would work.
 
 
 but substituting a copy of our actual svn access file I get
 
 Can't add new group cvs_cscda_owners: group already exists!
 
 So I may have a format issue with the example inout file
 
 Yeah, sounds like there's something potentially wrong with the input
 file - have you perhaps defined the cvs_cscda_owners group twice?
 


I think I found the problem at first I though it was the double spaces ( lines) 
between the repositories, but
after I commented out what listed properly I re-ran and got further down the 
list.

What I saw was a pattern that was if a repository had subdirectories listed but 
no group assigned the script stopped
When I commented those lines out or added groups  it continued.

example:

[code]
[foo::/]
@group = rw
@group2 = r

[foo:/branches]
[foo;/tags]
[foo:/trunk

'script stops here' 

example 2:

[/code]

[code]
[foo::/]
@group = rw
@group2 = r

[foo:/branches]
@group3 = rw
@group1 = r

[foo;/tags]
@grou4 = rw
@group2 = rrw

[foo:/trunk
@group5 = rw
@group6 = r

'script continues ok'  ---
[/code]


phil





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




Re: Newbie Where to start perhaps a hash-table is what I need to learn about ?

2012-07-05 Thread Phil Pinkerton

On Jul 5, 2012, at 10:46 AM, David Precious wrote:

 On Thu, 5 Jul 2012 09:30:01 -0400
 Phil Pinkerton pcpinker...@gmail.com wrote:
 ok using the example input file I got errors
 
 Use of uninitialized value $current_resource in string eq
 at /Library/Perl/5.12/SVN/Access.pm line 70, ACL line 3.
 [...]
 Invalid resource format in ! (format 'repo:/path')!
 
 Ah, I recall now I had to uncomment the #[groups] line in the example
 input you originally supplied before the script would work.
 
 
 but substituting a copy of our actual svn access file I get
 
 Can't add new group cvs_cscda_owners: group already exists!
 
 So I may have a format issue with the example inout file
 
 Yeah, sounds like there's something potentially wrong with the input
 file - have you perhaps defined the cvs_cscda_owners group twice?
 

The script also stops when a single user is entered instead of a group

This should not happen

example:

[code]

[foo:/]
@group1 = rw
@group2 = rw

[foo:/trunk]
userID = rI believe for Subversion this is a valid entrybut 
script stops when not a group

[/code]

phil


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




Re: Newbie Where to start perhaps a hash-table is what I need to learn about ?

2012-07-04 Thread Phil Pinkerton
Very nice to know about CPAN.

got a syntax error though with your script

syntax error at ./getACLinfo.pl line 51, near say join
Execution of ./getACLinfo.pl aborted due to compilation errors.

[code]
#!/usr/bin/perl

use strict;
use SVN::Access;
my $acl = SVN::Access-new( acl_file = 'data' );

# Walk through the resources in the ACL file:
for my $repopath ($acl-resources) {
   # For this resource, find out what authorisations exist:
   for my $auth ($repopath-authorized) {

   # $repopath-authorized gives us hashrefs of group = perms
   # TODO: I don't know if it might also give individual users, if
   # had per-user grants.
   my ($group, $perms) = each %$auth;

   if ($group =~ s/^@//) {
   # For each group, list out the members, along with the repo
   # permissions

   for my $user ($acl-group($group)-members) {
   say join '~', $user, $repopath-name, $group, $perms;
   }
   } else {
   die Can't handle individual user permissions!;
   }
   }
}
[/code]

On Jul 3, 2012, at 11:25 AM, David Precious wrote:

 On Tue, 3 Jul 2012 09:47:00 -0400
 Phil Pinkerton pcpinker...@gmail.com wrote:
 
 I was given a project that seems to require Perl
 
 I could use a sample just to extract a list of names associated with
 a group or repo and print them.
 
 1) Assigned a task to extract data fron a text file.
 2) Output file needs to be very specific, and created monthly
 3) tried doing in korn shell too complex
 4) Figured Perl my best option, but don't know Perl
 5) Got  the Modern Perl in PDF format
 6) Installed Strawberry Perl
 
 Output format is simple ??, but extracting the data and printing it
 I haven't a clue where to start. ( I am not trying to get someone to
 write this for me, just a simple extract and print example and point
 me in the right direction )
 
 First pointer - a lot of things you want to do in Perl will be a lot
 easier if you consult CPAN to see if someone has done this before and
 shared reusable code which will make your life easier.
 
 In this case, you've not described what the input data is, but to me it
 looks very much like a Subversion access control file.  So, with that
 in mind, I searched CPAN, and found SVN::Access:
 
 https://metacpan.org/module/SVN::Access
 
 Ahah - looks like it takes all the hard work out of things for you -
 it'll take care of the parsing, and let you just get at the data.
 
 So, glancing at its docs and whipping something up:
 
 [code]
 #!/usr/bin/perl
 
 use strict;
 use SVN::Access;
 my $acl = SVN::Access-new( acl_file = 'data' );
 
 # Walk through the resources in the ACL file:
 for my $repopath ($acl-resources) {
# For this resource, find out what authorisations exist:
for my $auth ($repopath-authorized) {
 
# $repopath-authorized gives us hashrefs of group = perms
# TODO: I don't know if it might also give individual users, if
# had per-user grants.
my ($group, $perms) = each %$auth;
 
if ($group =~ s/^@//) {
# For each group, list out the members, along with the repo
# permissions
 
for my $user ($acl-group($group)-members) {
say join '~', $user, $repopath-name, $group, $perms;
}
} else {
die Can't handle individual user permissions!;
}
}
 }
 [/code]
 
 If you install SVN::Access, the above should work for you, and should
 produce mostly what you need, e.g.:
 
 ritched2~repo1:/~repo1_devs~r
 appalas~repo1:/~repo1_devs~r
 
 Handing the _admn groups as a special case is left as an exercise to
 you, but should be reasonably easy given the above as a starting point.
 
 
 
 -- 
 David Precious (bigpresh) dav...@preshweb.co.uk
 http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter
 www.preshweb.co.uk/linkedinwww.preshweb.co.uk/facebook
 www.preshweb.co.uk/cpanwww.preshweb.co.uk/github
 
 
 -- 
 To unsubscribe, e-mail: beginners-unsubscr...@perl.org
 For additional commands, e-mail: beginners-h...@perl.org
 http://learn.perl.org/
 
 


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




Re: Newbie Where to start perhaps a hash-table is what I need to learn about ?

2012-07-04 Thread Chris Charley



Phil Pinkerton  wrote in message 
news:7a962da1-a5fb-4046-bbf5-f888dd715...@gmail.com...

Very nice to know about CPAN.

got a syntax error though with your script

syntax error at ./getACLinfo.pl line 51, near say join
Execution of ./getACLinfo.pl aborted due to compilation errors.

[code]
#!/usr/bin/perl

use strict;
use SVN::Access;
my $acl = SVN::Access-new( acl_file = 'data' );

# Walk through the resources in the ACL file:
for my $repopath ($acl-resources) {
  # For this resource, find out what authorisations exist:
  for my $auth ($repopath-authorized) {

  # $repopath-authorized gives us hashrefs of group = perms
  # TODO: I don't know if it might also give individual users, if
  # had per-user grants.
  my ($group, $perms) = each %$auth;

  if ($group =~ s/^@//) {
  # For each group, list out the members, along with the repo
  # permissions

  for my $user ($acl-group($group)-members) {
  say join '~', $user, $repopath-name, $group, $perms;
  }
  } else {
  die Can't handle individual user permissions!;
  }
  }
}
[/code]


Hello Phil,

Your script needs to use 5.010 or better to use the 'say' function.

Right after the 'use strict;' line, enter 'use 5.010;' or
a newer version if that is what you have.

Or, replace say with 'print':

print join('~', $user, $repopath-name, $group, $perms), \n;

Chris

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




Re: Newbie Where to start perhaps a hash-table is what I need to learn about ?

2012-07-04 Thread David Precious
On Wed, 4 Jul 2012 17:01:35 -0400
Phil Pinkerton pcpinker...@gmail.com wrote:

 Very nice to know about CPAN.

IMO, CPAN is one of Perl's strongest features :)

 
 got a syntax error though with your script
 
 syntax error at ./getACLinfo.pl line 51, near say join
 Execution of ./getACLinfo.pl aborted due to compilation errors.

Whoops - as Chris pointed out, 'say' requires perl 5.10 or newer; I
should have included 'use 5.010' in the script to indicate that, but
forgot to (I have $PERL5OPT set to '-M5.010' on most boxes).

Chris's mail contains full instructions to work round that problem.


-- 
David Precious (bigpresh) dav...@preshweb.co.uk
http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter
www.preshweb.co.uk/linkedinwww.preshweb.co.uk/facebook
www.preshweb.co.uk/cpanwww.preshweb.co.uk/github



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




Re: Newbie Where to start perhaps a hash-table is what I need to learn about ?

2012-07-04 Thread Phil Pinkerton
Wow I am impressed on the 4th of July no-less

I have revision 5 version 14 subversion 2 on linux  and v5.12.3 on my mac

On Jul 4, 2012, at 5:46 PM, Chris Charley wrote:

 
 
 Phil Pinkerton  wrote in message 
 news:7a962da1-a5fb-4046-bbf5-f888dd715...@gmail.com...
 Very nice to know about CPAN.
 
 got a syntax error though with your script
 
 syntax error at ./getACLinfo.pl line 51, near say join
 Execution of ./getACLinfo.pl aborted due to compilation errors.
 
 [code]
 #!/usr/bin/perl
 
 use strict;
 use SVN::Access;
 my $acl = SVN::Access-new( acl_file = 'data' );
 
 # Walk through the resources in the ACL file:
 for my $repopath ($acl-resources) {
  # For this resource, find out what authorisations exist:
  for my $auth ($repopath-authorized) {
 
  # $repopath-authorized gives us hashrefs of group = perms
  # TODO: I don't know if it might also give individual users, if
  # had per-user grants.
  my ($group, $perms) = each %$auth;
 
  if ($group =~ s/^@//) {
  # For each group, list out the members, along with the repo
  # permissions
 
  for my $user ($acl-group($group)-members) {
  say join '~', $user, $repopath-name, $group, $perms;
  }
  } else {
  die Can't handle individual user permissions!;
  }
  }
 }
 [/code]
 
 Hello Phil,
 
 Your script needs to use 5.010 or better to use the 'say' function.
 
 Right after the 'use strict;' line, enter 'use 5.010;' or
 a newer version if that is what you have.
 
 Or, replace say with 'print':
 
 print join('~', $user, $repopath-name, $group, $perms), \n;
 
 Chris
 
 -- 
 To unsubscribe, e-mail: beginners-unsubscr...@perl.org
 For additional commands, e-mail: beginners-h...@perl.org
 http://learn.perl.org/
 
 


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




Re: Newbie Where to start perhaps a hash-table is what I need to learn about ?

2012-07-04 Thread Phil Pinkerton

 
 Phil Pinkerton  wrote in message 
 news:7a962da1-a5fb-4046-bbf5-f888dd715...@gmail.com...
 Very nice to know about CPAN.
 
 got a syntax error though with your script
 
 syntax error at ./getACLinfo.pl line 51, near say join
 Execution of ./getACLinfo.pl aborted due to compilation errors.
 
 [code]
 #!/usr/bin/perl
 
 use strict;
 use SVN::Access;
 my $acl = SVN::Access-new( acl_file = 'data' );
 
 # Walk through the resources in the ACL file:
 for my $repopath ($acl-resources) {
  # For this resource, find out what authorisations exist:
  for my $auth ($repopath-authorized) {
 
  # $repopath-authorized gives us hashrefs of group = perms
  # TODO: I don't know if it might also give individual users, if
  # had per-user grants.
  my ($group, $perms) = each %$auth;
 
  if ($group =~ s/^@//) {
  # For each group, list out the members, along with the repo
  # permissions
 
  for my $user ($acl-group($group)-members) {
  say join '~', $user, $repopath-name, $group, $perms;
  }
  } else {
  die Can't handle individual user permissions!;
  }
  }
 }
 [/code]
 
 Hello Phil,
 
 Your script needs to use 5.010 or better to use the 'say' function.
 
 Right after the 'use strict;' line, enter 'use 5.010;' or
 a newer version if that is what you have.
 
added use 5.12.3

made some progress but still have errors
$ ./getACLinfo.pl 
Use of uninitialized value $current_resource in string eq at 
/Library/Perl/5.12/SVN/Access.pm line 70, ACL line 3.
Use of uninitialized value $current_resource in string eq at 
/Library/Perl/5.12/SVN/Access.pm line 73, ACL line 3.
Use of uninitialized value $resource_name in string eq at 
/Library/Perl/5.12/SVN/Access.pm line 259, ACL line 3.
Use of uninitialized value $resource_name in pattern match (m//) at 
/Library/Perl/5.12/SVN/Access.pm line 265, ACL line 3.
Use of uninitialized value $resource_name in concatenation (.) or string at 
/Library/Perl/5.12/SVN/Access.pm line 266, ACL line 3.
Invalid resource format in ! (format 'repo:/path')!

#!/usr/bin/perl
[code]
use strict;
use 5.12.3;
use SVN::Access;
my $acl = SVN::Access-new( acl_file = 
'/Users/pnkerton/scripts/ACLexample.txt' );

# Walk through the resources in the ACL file:
for my $repopath ($acl-resources) {
   # For this resource, find out what authorisations exist:
   for my $auth ($repopath-authorized) {

   # $repopath-authorized gives us hashrefs of group = perms
   # TODO: I don't know if it might also give individual users, if
   # had per-user grants.
   my ($group, $perms) = each %$auth;

   if ($group =~ s/^@//) {
   # For each group, list out the members, along with the repo
   # permissions

   for my $user ($acl-group($group)-members) {
   say join ~, $user, $repopath-name, $group, $perms;
   }
   } else {
   die Can't handle individual user permissions!;
   }
   }
}
[/code]


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




Re: Newbie Where to start perhaps a hash-table is what I need to learn about ?

2012-07-03 Thread David Precious
On Tue, 3 Jul 2012 09:47:00 -0400
Phil Pinkerton pcpinker...@gmail.com wrote:

 I was given a project that seems to require Perl
 
 I could use a sample just to extract a list of names associated with
 a group or repo and print them.
 
 1) Assigned a task to extract data fron a text file.
 2) Output file needs to be very specific, and created monthly
 3) tried doing in korn shell too complex
 4) Figured Perl my best option, but don't know Perl
 5) Got  the Modern Perl in PDF format
 6) Installed Strawberry Perl
 
 Output format is simple ??, but extracting the data and printing it
 I haven't a clue where to start. ( I am not trying to get someone to
 write this for me, just a simple extract and print example and point
 me in the right direction )

First pointer - a lot of things you want to do in Perl will be a lot
easier if you consult CPAN to see if someone has done this before and
shared reusable code which will make your life easier.

In this case, you've not described what the input data is, but to me it
looks very much like a Subversion access control file.  So, with that
in mind, I searched CPAN, and found SVN::Access:

https://metacpan.org/module/SVN::Access

Ahah - looks like it takes all the hard work out of things for you -
it'll take care of the parsing, and let you just get at the data.

So, glancing at its docs and whipping something up:

[code]
#!/usr/bin/perl

use strict;
use SVN::Access;
my $acl = SVN::Access-new( acl_file = 'data' );

# Walk through the resources in the ACL file:
for my $repopath ($acl-resources) {
# For this resource, find out what authorisations exist:
for my $auth ($repopath-authorized) {

# $repopath-authorized gives us hashrefs of group = perms
# TODO: I don't know if it might also give individual users, if
# had per-user grants.
my ($group, $perms) = each %$auth;

if ($group =~ s/^@//) {
# For each group, list out the members, along with the repo
# permissions

for my $user ($acl-group($group)-members) {
say join '~', $user, $repopath-name, $group, $perms;
}
} else {
die Can't handle individual user permissions!;
}
}
}
[/code]

If you install SVN::Access, the above should work for you, and should
produce mostly what you need, e.g.:

ritched2~repo1:/~repo1_devs~r
appalas~repo1:/~repo1_devs~r

Handing the _admn groups as a special case is left as an exercise to
you, but should be reasonably easy given the above as a starting point.



-- 
David Precious (bigpresh) dav...@preshweb.co.uk
http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter
www.preshweb.co.uk/linkedinwww.preshweb.co.uk/facebook
www.preshweb.co.uk/cpanwww.preshweb.co.uk/github


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




Re: Newbie needs help

2011-03-22 Thread shawn wilson
On Tue, Mar 22, 2011 at 1:05 PM, Geospectrum alan.hunt...@geospectrum.co.uk
 wrote:

 Hi am am setting up a small website and have set up formmail.pl to
 create a way of people asking questions.

 I'd like now to add a way of visitors to display a HTML page by
 entering a number (invoice number) into a HTML form and then retrieve
 a html page. So I create a html page called http://www.mysite/1234.html
 and the user types in 1234 into the form and the page
 http://www.mysite/1234.html
 is retrived and displayed.

 I imagine I could use the perl script to create the full url and
 display the page by conctenating the domain name, invoice number
 and .html but I really have no idea about how to do this. Can I use
 the GET method of the form? I am hoping someone can point me towards a
 tutorial or offer up some snippets.


 lets go simple and then go pro here.

simple (bad) use cgi get. so www.example.com/script.pl?1234

and do something like (untested, should work):
my $page = $cgi-param( 'page' );
print $cgi-redirect( $page . .html );

however, you probably want to use template toolkit if you're doing simple
perl pages. personally, i use catalyst and javascript to render data (so i
don't do too much in template toolkit).

if you do catalyst, you might setup a model that looks something like this:
my $page = $c-req-param-{page};
$c-forware( View:: . $page );

which would forward you to that tt view when you get that request. i don't
like either of these ways of doing it and if you wanted to do exactly as you
asked, just open a file handle and dump. though, my preference really is to
return json data and use js to render :)

also, this is more meta code than code - you need to do checking and what
not. i'm not sure if cgi.pm does anything to prevent me from doing:
www.example.com/script.pl?page=rm%20-rf%20whatever the symbol is for
'/'%20
which may do something like:
rm -rf / .html
(html wouldn't match but / would - not good)


Re: Newbie needs help

2011-03-22 Thread Randal L. Schwartz
 Geospectrum == Geospectrum  alan.hunt...@geospectrum.co.uk writes:

Geospectrum Hi am am setting up a small website and have set up formmail.pl to
Geospectrum create a way of people asking questions.

I'm so very much hoping that you're not using Matt Wright's original
formmail.pl, and instead using the one from NMS.  Even Matt now tells
you to go get NMS:

   http://www.scriptarchive.com/nms.html

Yes, Matt's a good guy... even he knows when he's been beaten. :)

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
mer...@stonehenge.com URL:http://www.stonehenge.com/merlyn/
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion

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




Re: Newbie queries

2011-01-27 Thread dolphin
On Jan 25, 6:43 pm, orasn...@gmail.com (Octavian Rasnita) wrote:
 From: dolphin yc282...@yahoo.com.sg









  Hi,

  I'm learning perl now and would like to find out how to perform the
  following if given in a text file?

  ,12
  ,437
  ,124
  ,45
  ,789
  ,67
  CCC,567
  DDD,5

  How to sum up the 2nd column based on the 1st column with the
  following result:

  :
  BBB:
  CCC:
  DDD:

  Thanks in advance.

 You need to read the file line by line:

 use strict;
 use warnings;

 my %items;    #The hash with labels and their corresponding sums

 open my $file, '', 'file_name' or die $!;

 while ( my $line = $file ) {

     # Here split the line:
     my ( $label, $value ) = split /,/, $line, 2;

     # Then add each value for its corresponding label:
     $items{$label} += $value;

 }

 close $file;

 # Here print the sorted hash of items:
 for my $label ( sort keys %items ) {
     print $label: $items{$label}\n;

 }

 HTH.

 Octavian

Hi,

Correct me that the 2 in the following means read?
:
my ( $label, $value ) = split /,/, $line, 2;


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




Re: Newbie queries

2011-01-27 Thread Octavian Rasnita

From: dolphin yc282...@yahoo.com.sg

Hi,

Correct me that the 2 in the following means read?
:
my ( $label, $value ) = split /,/, $line, 2;



2 means that the result of the split() function will be a list with 2 
elements.
You could also use the split() function without that third parameter that 
specifies the number of the returned elements:


my ( $label, $value ) = split /,/, $line;

But the first variant is a little bit faster.

Read:

perldoc -f split

Octavian



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




Re: Newbie queries

2011-01-25 Thread Octavian Rasnita

From: dolphin yc282...@yahoo.com.sg

Hi,

I'm learning perl now and would like to find out how to perform the
following if given in a text file?


,12
,437
,124
,45
,789
,67
CCC,567
DDD,5


How to sum up the 2nd column based on the 1st column with the
following result:


:
BBB:
CCC:
DDD:


Thanks in advance.



You need to read the file line by line:


use strict;
use warnings;

my %items;#The hash with labels and their corresponding sums

open my $file, '', 'file_name' or die $!;

while ( my $line = $file ) {

   # Here split the line:
   my ( $label, $value ) = split /,/, $line, 2;

   # Then add each value for its corresponding label:
   $items{$label} += $value;
}

close $file;

# Here print the sorted hash of items:
for my $label ( sort keys %items ) {
   print $label: $items{$label}\n;
}

HTH.

Octavian


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




Re: Newbie queries

2011-01-25 Thread Mariano Loza Coll
On Tue, Jan 25, 2011 at 2:43 AM, Octavian Rasnita orasn...@gmail.com wrote:
 From: dolphin yc282...@yahoo.com.sg

 Hi,

 I'm learning perl now and would like to find out how to perform the
 following if given in a text file?


 ,12
 ,437
 ,124
 ,45
 ,789
 ,67
 CCC,567
 DDD,5


 How to sum up the 2nd column based on the 1st column with the
 following result:


 :
 BBB:
 CCC:
 DDD:


 Thanks in advance.


 You need to read the file line by line:


 use strict;
 use warnings;

 my %items;    #The hash with labels and their corresponding sums

 open my $file, '', 'file_name' or die $!;

 while ( my $line = $file ) {

   # Here split the line:
   my ( $label, $value ) = split /,/, $line, 2;

   # Then add each value for its corresponding label:
   $items{$label} += $value;
 }

 close $file;

 # Here print the sorted hash of items:
 for my $label ( sort keys %items ) {
   print $label: $items{$label}\n;
 }

 HTH.

 Octavian


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




Thanks Octavian. I'm also new to Perl and yours was a very simple and
useful explanation.
Cheers,

Mariano

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




Re: Newbie: Perl how evaluate files newer than an hour within ftp

2010-08-18 Thread Shlomi Fish
Hi Atropo,

On Tuesday 17 August 2010 15:37:51 Atropo wrote:
 Hi all, i have this simple script to check if a file has arrive since
 the last hour
 
 find2perl tmp -type f  -eval '-M $_  1/24' -print |perl
 
 but now i would like to check in a remote server, maybe on ftp
 session. i have this ftp.pl
 
 perl -w  ftp.log -MNet::FTP -le'

Please:

1. Don't write your program as a gigantic -e '...' block. Instead - put it in 
a separate file. You can't honestly expect us to debug it.

2. See https://www.socialtext.net/perl5/index.cgi?ancient_perl

3. use strict; and use warnings;.

4. Quoting perlbot:

{{{
perlbot use strict; use warnings; use lexical filehandles instead of globals 
(open my $tmp ..vs.. open TMP), use three argument form of open, don't use 
prototypes unless you really want to. don't call subs with  unless it's 
required (sub() ..vs.. sub()). check for errors (open (); ..vs..  open() or 
die $!)
}}}

 ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek
 +, $dayOfYear, $daylightSavings) = localtime();

You should use a good datetime module instead. I like 
http://search.cpan.org/dist/DateTime/ but there are others.

 $year = 1900 + $yearOffset;
 $theGMTime = $hour:$minute:$second, $months[$month] $dayOfMonth,
 $year;
  ( $host, $user, $pass, $dir ) = @ARGV;
  $ftp = Net::FTP-new($host) or die $...@\n;  #send mail if cannot
 connect do not know how to do it
  $ftp-login( $user, $pass ) or die $ftp-message;
  $ftp-cwd($dir) or die $ftp-message;
  $ftp-binary;
  for $file ($ftp-ls){
  push @files, $file;
  }
 
  for $file (@files) {
$quesesto=localtime($ftp-mdtm($file));
$localtimenoformat=localtime(time());
$mdtmnoformat=$ftp-mdtm($file);
print MDTM with FORMAT=  $quesesto\n;
print Localtime No Format =  $localtimenoformat\n;
print MDTM  No Format =  $mdtmnoformat\n;
  #envia_mail ();
  }
  $ftp-quit or die $ftp-message;
 
 sub envia_mail {
 print Content-type: text/html\n\n;
 
 #$title='';
 $to=alexis_vasqu...@codetel\.com\.do;
 $from= `hostname`;
 #$subject='';
 
 open(MAIL, |/usr/sbin/sendmail -t);
 
 ## Mail Header
 print MAIL To: $to\n;
 print MAIL From: $from\n;
 print MAIL Subject: $subject\n\n;
 ## Mail Body
 print MAIL This is a test message \n;
 
 close(MAIL);
 
 print htmlheadtitle$title/title/head\nbody\n\n;
 
 ## HTML content sent, let use know we sent an email
 print 
 h1$title/h1
 
 A message has been sent from $from to $to
 
 /body/html;


You should use a here-document here.

 
 }
 
 'host user pass dir

Don't append the «host» parameter to the end of hte '' - it will be part 
of the program.

 
 Still cant't figure out how to check if file is newer that an hour. I
 can get this:
 
 MDTM with FORMAT = Fri Aug 21 12:23:15 2009
 Localtime No Format = Fri Aug 13 10:17:10 2010
 MDTM No Format = 1250871795
 
 But don't know how to get the 'localtime with NO Format' to compare
 with the 'MDTM with NO Format'

Just use timestamps, or use the DateTime module. You can find if a timestamp 
is in the last hour by time() - $timestamp  60*60.

Regards,

Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
The Human Hacking Field Guide - http://shlom.in/hhfg

God considered inflicting XSLT as the tenth plague of Egypt, but then
decided against it because he thought it would be too evil.

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: NEWBIE: How to write to a file

2010-06-17 Thread Uri Guttman
 A == Atropo  lxvasq...@gmail.com writes:

  A Hi all,don't know almost nothing about perl but I'm RTFM.
  A Dimitre Radoulov gave a perl script to get and delete via ftp some
  A files. now i want generate a log file with those file names.

  A I only added the lines starting in uppercase

who told you that you can write perl functions in upper case? perl is
case sensitive and all the functions are in lower case.

  A perl -MNet::FTP -le'
  A  ( $host, $user, $pass, $dir ) = @ARGV;
  A  $MY_FILE  = recibidos.txt;
  A  OPEN(PLOT,$MY_FILE) or die(The file cannot be opened!);

add $! to the die so you see why it can't be opened. also add the
filename to that string.

  A  $ftp = Net::FTP-new($host) or die $...@\n;
  A  $ftp-login( $user, $pass ) or die $ftp-message;
  A  $ftp-cwd($dir) or die $ftp-message;
  A  $ftp-binary;
  A  /(.*)\.tag$/ and $files_ko{$1} = 1
  Aor push @files, $_
  Afor $ftp-ls;
  A  for (@files) {
  A  $ftp-get($_) and $ftp-delete($_)
  Aunless exists $files_ko{$_};
  A CHOMP;

why are you chomping that? ftp doesn't return file names with newlines
(that only happens when you read lines from a file or ls output, etc.)

  A PRINT PLOT $_\n;
  A  }

  A Undefined subroutine main::OPEN called at -e line 4.

that is because there is no OPEN function. there is an open
function. you claim to be rtfming but again, where did you get the idea
that upper case functions are supported?

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -

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




Re: Newbie stuck at the first hurdle

2010-04-24 Thread Kryten
Hi Uri,

Thanks for the response.

I was using both Komodo Edit and Primalscript and
didn't see a compile error, until I added the ref to $!
then I could see that my filepath was being damaged
because of the doublequotes.

The reason I wondered about the full path was because I
have been using Powershell an awful lot these last two years
and I am aware of certain scenarios where sometimes you
have to supply a full path and sometimes you only have to
supply a path relative to where you have cd into, or in the
case of Powershell set-location, though cd is an alias.
Whereas something like [system.io.file]::ReadALLLines($path)
where $path must be the full path.

Sorry for any confusion about the use of /* */, thats just a bad
habit.

I've only just installed and begun to learn Perl. The books arrived
today
so I'm looking forward to the journey!

Thanks again,

Stuart


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




Re: Newbie stuck at the first hurdle

2010-04-24 Thread Kryten
Thank you Thomas.

The problem was indeed the use of a double quoted string
which was wrecking my file path. Using the $! var helped me
to see that.

Learning Perl and Programming Perl both arrived today, thank
goodness!

Cheers,
Stuart


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




Re: Newbie stuck at the first hurdle

2010-04-22 Thread Uri Guttman
 K == Kryten  kryte...@googlemail.com writes:

  K All I want to do is read the content of a simple 10 line .txt file
  K into an array and print out the lines, and I just can't seem to get
  K it to work!

  K /*
  K $target = D:\testfile.txt;
  K open NEWBIE, $target;

that won't even compile. are you sure you aren't seeing a compiler
error?

and check opens for errors:

open NEWBIE, $target or die can't open $target $! ;

  K @arr = WAWA;
  K foreach $line (@arr) {
  K print $line\n;

your lines already have newlines so no need to print another.


  K }
  K close(NEWBIE)
  K */

why the c style comments here? we can tell code from your email text.

  K Is it ok to reference the full path in the 2nd arg to 'open'?

why wouldn't it? paths are paths, relative or absolute. if the path is
good, the file can be usually found and opened (given proper permissions).

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -

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




Re: Newbie stuck at the first hurdle

2010-04-22 Thread Owen

 Hi,

 This is embarrassing.

 All I want to do is read the content of a simple 10 line .txt file
 into an array
 and print out the lines, and I just can't seem to get it to work!
 /*
 $target = D:\testfile.txt;
 open NEWBIE, $target;
 @arr = WAWA;
 foreach $line (@arr) {
 print $line\n;
 }
 close(NEWBIE)
 */


You have an @arr = WAWA above Anyway, try this, (not tested on
Windows)



#

#/use/bin/perl

use strict;  # always use strict
use warnings;# helps in debugging

my $target = D:\testfile.txt;

open (my $NEWBIE, '', $target) or die can't open $target $!\n;

my @arr = $NEWBIE;
foreach my $line (@arr){
print $line; # no need for a new line here
 }


#
-- 



Owen


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




Re: newbie question : about the perl sprintf

2009-10-25 Thread Majian
I found these :
perl -e'print 01.234 + 01.234', \n'
perl -e'print 01.234 + 011.234' \n'
perl -e'print 01.234.12 + 01.234', \n'

And the results were :

1235234
1235234
1235.12234



Can someone explain it ?

Thanks~~


On Sat, Oct 24, 2009 at 7:28 PM, Peter Scott pe...@psdt.com wrote:

 On Wed, 21 Oct 2009 20:52:05 +0800, Majian wrote:
  And  I modify it like this sprintf The number in
  scientific
  notation is %e, 01.255;
  The screen now output is  The number in scientific
  notation
  is 1.255000e+03

 Ha, this is an interesting case.  By putting the zero before the 1, it
 turns it into an octal number and now the period becomes the concatenation
 operator instead of a decimal point, yielding a term of 1255.

 Try printf The number in scientific notation is %e, 037.255; and see
 what happens.

 --
 Peter Scott
 http://www.perlmedic.com/
 http://www.perldebugged.com/
 http://www.informit.com/store/product.aspx?isbn=0137001274

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





Re: newbie question : about the perl sprintf

2009-10-25 Thread Philip Potter
2009/10/25 Majian jian...@gmail.com:
 I found these :
 perl -e'print 01.234 + 01.234', \n'

print (01).(234+01).234, \n;

this evaluates to '1'.'235'.'234'

 perl -e'print 01.234 + 011.234' \n'

I didn't get 1235234, I got 1243234.
print (01).(234+011).(234),\n
evaluates to
print '1'.(234+9).'234',\n;
evaluates to
print '1'.'243'.'234',\n;

 perl -e'print 01.234.12 + 01.234', \n'

I'll let you work this one out as an exercise. The key point is that
you can't have decimal octal numbers, so the . operator is interpreted
as the string concatenation operator.

Phil

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




Re: newbie question : about the perl sprintf

2009-10-25 Thread Dr.Ruud

Majian wrote:

I found these :
perl -e'print 01.234 + 01.234', \n'
perl -e'print 01.234 + 011.234' \n'
perl -e'print 01.234.12 + 01.234', \n'

And the results were :

1235234
1235234
1235.12234


For other surprises, try also:

perl -wle 'print length(01.234.12)'
perl -wle 'print 01.234.12'

perl -wle 'print length(1.234.12)'
perl -wle 'print length(0+1.234.12)'

--
Ruud

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




Re: newbie question : about the perl sprintf

2009-10-24 Thread Peter Scott
On Wed, 21 Oct 2009 20:52:05 +0800, Majian wrote:
 And  I modify it like this sprintf The number in
 scientific
 notation is %e, 01.255;
 The screen now output is  The number in scientific
 notation
 is 1.255000e+03

Ha, this is an interesting case.  By putting the zero before the 1, it 
turns it into an octal number and now the period becomes the concatenation 
operator instead of a decimal point, yielding a term of 1255.

Try printf The number in scientific notation is %e, 037.255; and see 
what happens.

-- 
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/
http://www.informit.com/store/product.aspx?isbn=0137001274

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




Re: newbie question : about the perl sprintf

2009-10-22 Thread uap12
On 21 Okt, 14:52, jian...@gmail.com (Majian) wrote:
 Hi, all ;

            I want to print  this sentence  The number in scientific
 notation is 1.255000e+02.

          So   I write a perl script like this :
                #!/usr/bin/perl
                 sprintf The number in scientific notation is %e, 1.255;
            But  the screen output is The number in scientific notation is
 1.255000e+00

             And  I modify it like this sprintf The number in scientific
 notation is %e, 01.255;
                 The screen now output is  The number in scientific notation
 is 1.255000e+03

            I don't know how I can display the correct answer .

       Please forgive me if this is a very simple question on Perl . I am a
 Perl newbie ~~~

           Thanks ~~~

#!/usr/bin/perl
printf(The number in scientific notation is %e\n, 125.55);
Gives The number in scientific notation is 1.255500e+002

I test to enter the result you are looking for as

perl -42 de  now ju can test single line of Perl kode

heter i enter print 1.255500e+02

( CTRL-Z) takes you out, (att least in WIndows version of PERL)

You can read more about formating data in this link
http://perldoc.perl.org/functions/sprintf.html

Best regards
Anders


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




Re: newbie question : about the perl sprintf

2009-10-21 Thread Jim Gibson

At 8:52 PM +0800 10/21/09, Majian wrote:

Hi, all ;

   I want to print  this sentence  The number in scientific
notation is 1.255000e+02.

 So   I write a perl script like this :
   #!/usr/bin/perl
sprintf The number in scientific notation is %e, 1.255;
   But  the screen output is The number in scientific notation is
1.255000e+00


I don't get anything when I execute that line. You must have used the 
printf function in your actual code.


1.255000e+00 is the correct scientific representation of the number 
1.255. If you want to print 1.255000e+02, then you should print the 
number 125.5.



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




Re: newbie question : about the perl sprintf

2009-10-21 Thread Christian Bernini

 sprintf The number in scientific notation is %e, 1.255;


Sprintf doesn't print actually, it just returns a string based on the
formats provided in the list you have.

To actually print something you should use printf.

printf The number in scientific notation is %e,1255;


And the notation 1.255000e+03 is right as the output since it means 1.255000
* (10^3), so

1.255 * 1000 = 1255.

(as Jim already said)

Att.
Christian Bernini


Re: newbie needs help with first perl prog

2009-05-08 Thread John W. Krahn

Prince Mavi wrote:

Hi folks


Hello,


I am new to perl. this is my first real program in perl.
The program is not doing what i intend it to do.
The problem in nut shell is that:
I expect to see the menu first and then input my choice.
but
the program asks for my choice first and then displays the menu

Please have a look and see what am i doing wrong.

Thanks a ton for your time guys.


code
--
#!/usr/bin/perl
use strict;
use warnings;

sub dispBlanks {
print \n\n;
}

sub clrScr {
system(clear);
}

sub dispMenu {
clrScr;
print \n\n=== Student Manage Pro ===\n;
print 1. Display all students\n;
print 2. Display a particular student\n;
print 3. Add a new student\n;
print 4. Delete a student\n;
print 0. Exit\n;
print Please choose one of the options\n;
}

while (1) {
dispMenu
my $choice = STDIN;


Your problem is here.  You are calling the subroutine 'dispMenu' with 
the argument 'my $choice = STDIN'.  In order to pass the value of the 
expression 'my $choice = STDIN' to the subroutine 'dispMenu' the 
expression must be evaluated first so you must provide an option before 
the menu is displayed.




chomp($choice); # to remove newline
print you chose $choice;


Once that is fixed then this line will only diplay for a tiny fraction 
of a second as you loop around and clear the screen right away.




dispBlanks;
} 
--


I would do it using strings instead of subroutines:

# #!/usr/bin/perl
use strict;
use warnings;

my $dispBlanks = \n\n;
my $clrScr = `clear`;
my $dispMenu   = MENU;
$clrScr


=== Student Manage Pro ===
1. Display all students
2. Display a particular student
3. Add a new student
4. Delete a student
0. Exit
Please choose one of the options
MENU

while ( 1 ) {
print $dispMenu;
my $choice = STDIN;
chomp $choice;  # to remove newline
last if $choice == 0;
print you chose $choice, $dispBlanks;
}

__END__



John
--
Those people who think they know everything are a great
annoyance to those of us who do.-- Isaac Asimov

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




Re: newbie needs help with first perl prog

2009-05-07 Thread Rhinux.xu
Prince Mavi 写道:
 Hi folks

 I am new to perl. this is my first real program in perl.
 The program is not doing what i intend it to do.
 The problem in nut shell is that:
 I expect to see the menu first and then input my choice.
 but
 the program asks for my choice first and then displays the menu

 Please have a look and see what am i doing wrong.

 Thanks a ton for your time guys.


 code
 --
 #!/usr/bin/perl
 use strict;
 use warnings;

 sub dispBlanks {
 print \n\n;
 }

 sub clrScr {
   system(clear);
 }

 sub dispMenu {
 clrScr;
 print \n\n=== Student Manage Pro ===\n;
 print 1. Display all students\n;
 print 2. Display a particular student\n;
 print 3. Add a new student\n;
 print 4. Delete a student\n;
 print 0. Exit\n;
 print Please choose one of the options\n;
 }

 while (1) {
 dispMenu
 my $choice = STDIN;
 chomp($choice);   # to remove newline
 print you chose $choice;
 dispBlanks;
 } 
 --


   
1 #!/usr/bin/perl -w
2 use strict;
3 use warnings;
4
5 sub dispBlanks {
6 print \n\n;
7 }
8
9 sub clrScr {
10 system(clear);
11 }
12
13 clrScr;/ #=
14 sub dispMenu {
15 print \n\n=== Student Manage Pro ===\n;
16 print 1. Display all students\n;
17 print 2. Display a particular student\n;
18 print 3. Add a new student\n;
19 print 4. Delete a student\n;
20 print 0. Exit\n;
21 print Please choose one of the options\n;
22 }
23
24 my $choice = 1;
25 while ($choice) {
26 dispMenu;
27 chomp($choice = STDIN);
28 print you chose $choice;
29 dispBlanks;
30 }

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




Re: newbie needs help with first perl prog

2009-05-07 Thread Steve Bertrand
Prince Mavi wrote:
 Hi folks
 
 I am new to perl. this is my first real program in perl.
 The program is not doing what i intend it to do.
 The problem in nut shell is that:
 I expect to see the menu first and then input my choice.
 but
 the program asks for my choice first and then displays the menu
 
 Please have a look and see what am i doing wrong.
 
 Thanks a ton for your time guys.

Changes that I found to make it work:

#!/usr/bin/perl
use strict;
use warnings;

sub dispBlanks {
print \n\n;
}

sub clrScr {
system(clear);
}

sub dispMenu {
#clrScr;  # -- commented out
print \n\n=== Student Manage Pro ===\n;
print 1. Display all students\n;
print 2. Display a particular student\n;
print 3. Add a new student\n;
print 4. Delete a student\n;
print 0. Exit\n;
print Please choose one of the options\n;
}

while (1) {
dispMenu(); #  missing semi-colon
my $choice = STDIN;
chomp($choice); # to remove newline
print you chose $choice;
dispBlanks;
}

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




Re: newbie with a question on syntax

2009-02-04 Thread Dr.Ruud

Chas. Owens wrote:

 [fat comma]

it treats the thing on its left
like a string if the thing on the left matches this pattern
/^[-_a-zA-Z][-\w]*$/


I don't believe that pattern. So lets test:


perl -wle '
  $,= = ;
  my %h = ( -- = x);
  print %h;
'
syntax error at -e line 3, near -- =
Execution of -e aborted due to compilation errors.

Probably you meant the second character class to be just \w?
Even then the pattern is not right:


perl -wle '
  $,= = ;
  my %h = ( 1.23 = x);
  print %h;
'
1.23 = x

--
Ruud

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




Re: newbie with a question on syntax

2009-02-04 Thread Chas. Owens
On Tue, Feb 3, 2009 at 18:32, Dr.Ruud rvtol+use...@isolution.nl wrote:
 Chas. Owens wrote:

 [fat comma]

 it treats the thing on its left
 like a string if the thing on the left matches this pattern
 /^[-_a-zA-Z][-\w]*$/

 I don't believe that pattern. So lets test:


 perl -wle '
  $,= = ;
  my %h = ( -- = x);
  print %h;
 '
 syntax error at -e line 3, near -- =
 Execution of -e aborted due to compilation errors.

 Probably you meant the second character class to be just \w?
 Even then the pattern is not right:


 perl -wle '
  $,= = ;
  my %h = ( 1.23 = x);
  print %h;
 '
 1.23 = x

Yep, the Perl interpreter in my head is buggy again.


-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

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




Re: newbie with a question on syntax

2009-02-03 Thread Chas. Owens
On Mon, Feb 2, 2009 at 16:43, Tina tin...@gmail.com wrote:
 Hello, I just started programming in perl and currently am working in
 an OOP sort of program. I am writing the constructor and I wasn't sure
 of the differences between the following syntax:

 'count' = 0

 @count = 0

 Both seem to work fine, but I would like to know how they are
 different and whether or not one is better or more proper for
 programming in Perl.

 Can someone explain this to me?

The = operator* is called the fat-comma operator.  It can be used in
the same places a comma could be used.  Its normal usage is to denote
pairs of items to the programmer (Perl doesn't care).  For instance,
it is used here (along with some whitespace) to denote the key/value
relationship of the items being assigned to %hash.

my %hash = (
key1 = val1,
key2 = val2,
);

Compare this to the equivalent comma based code.

my %hash = (
key1, val1,
key2, val2,
);

Most find the first more aesthetically pleasing, but Perl doesn't
care; they both compile down to the same opcodes.  It has one special
property when compared with comma: it treats the thing on its left
like a string if the thing on the left matches this pattern
/^[-_a-zA-Z][-\w]*$/, so there is a difference in the compile-time
behaviour, but not the runtime behaviour.

The = operator** is called the assignment operator.  It puts a value
into a variable***:

my $scalar = 5;
my @array  = (1, 2, 3, 4, $scalar);

It is important to note that you should avoid saying

@count = 0;

For, while this may work with one value, the assignment operator has a
high precedence than the comma operator.  This means when you say

@count = 0, 1, 2, 3;

Perl sees this

(@count = 0), 1, 2, 3;

Almost always group items being assigned to an array:

@count = (0, 1, 2, 3);

Exceptions to this rule include copying one array into another and
assigning the results of one function to an array.

A final note, based nature of the assignment you wrote, it appears as
if you are not using the strict pragma.  The strict and warnings
pragmas are of great importance in modern Perl programming and should
always be used.  You can activate these pragmas by saying

use strict;
use warnings;

at the top of you program after the #! line.  You may find that you
get a lot of errors when you do this.  The errors strict is
complaining about are indeed errors even when strict is not in place;
it is just that Perl lets you get away with them.  This is roughly the
same as speeding when no cops are around.

* http://perldoc.perl.org/perlop.html#Comma-Operator
** http://perldoc.perl.org/perlop.html#Assignment-Operators
*** Well, really it puts an rvalue into an lvalue, but that is a more
complicated discussion

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

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




Re: Newbie question about variables, arrays and where they all go

2009-01-19 Thread dolphin_sonar
On Jan 18, 7:54 pm, telemac...@arpinum.org (Telemachus) wrote:
 On Sun Jan 18 2009 @ 10:59, dolphin_sonar wrote:

     1 # When calling 'running_sum(5, 6);' the variable 'state @numbers'
  receives those two
     2 # parameters, (5 and 6), right? Then, the @numbers array also
  copies/stores (5 and 6)
     3 # into the special '( @_ )' variable as well, right? Also, line
  13 pushes '$number' into
     4 # the '@numbers' array each time through the foreach loop,
  right?

 Maybe I'm not understanding what you mean, but I think you're confused. The
 arguments to a subroutine go into the @_ array. The @numbers array is
 empty until you load it up in the foreach loop. Having 5 and 6 as arguments
 does not automatically put those items into @numbers, nor does @numbers
 copy anything into @_.

 Also, I understand that you may just be testing out persistent variables in
 5.10, but this program is confusing.

 Consider what happens if you call running sum a second time (say as
 running_sum( 2, 7 );) If I add that call, here's my output:

     telemachus ~ $ perl sum
     The sum of (5 6) is 11
     The sum of (5 6 2 7) is 31

 That appears to say that perl has added 5, 6, 2, and 7 up to 31. What
 actually happened was that you added 5, 6, 2 and 7 (20) to 11 (the sum from
 the previous call to running_sum - which was saved). To put this another way
 around: maybe you want to keep a running sum, but the print statement in the
 subroutine is very confusing.

 Hope this helps, T

T!

Again, you answered my question. You should be teaching Perl as your
responses are very clear and that's not easy for people to do...give
clear, concise responses.

Regardless, I now understand exactly what your saying about the state
@numbers declaration. I was confused about that and that helps me
understand a lot of what is going on, that was important for me to
understand. The example code actually had two more calls to running
sum: running_sum( 1..3 ); and running_sum( 4 ); (see the link to the
code and the out below).

Here is a link to the example code in a much better format, you can
see the code better than using Gmail IMO. http://sial.org/pbot/34575

# The output I get each time is this:
The sum of (5 6) is 11
The sum of (5 6 1 2 3 ) is 17
The sum of (5 6 1 2 3 4 ) is 21

This example comes from page 68 O'Reilly Learning Perl 5th edition.
They also used 'say' for the first time in the book instead of print
as well, which was kind of confusing to me.


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




Re: Newbie question about variables, arrays and where they all go

2009-01-19 Thread Telemachus
On Mon Jan 19 2009 @  4:21, dolphin_sonar wrote:
 Again, you answered my question. You should be teaching Perl as your
 responses are very clear and that's not easy for people to do...give
 clear, concise responses.

Actually, I had a big goof in my response. The program and the print
statement are fine as you had them, and *I* got myself turned around.

When I typed up your program, I was thinking of other ways I might do it,
and I produced this:

sub running_sum {
state $sum = 0;
state @numbers;

push @numbers, @_;
$sum += $_ foreach @numbers;  ## WRONG - should be foreach @_
say The sum of (@numbers) is $sum;
}

I thought it would be easier to push the whole @_ array into @numbers at
once and add everything into $sum in a one-liner as well. But I did it
wrong.

The problem was that I added the entire @numbers array to $sum, but from
from call to call, I only want to add the new items (the ones in the @_
array). That's what I get for trying to answer a question and rewrite the
example all at the same time.

As for 'say' it's a new feature in 5.10 (like the state variables) which
allows you to print without explicitly asking for the \n - it looks like
it's officially introduced in Learning Perl 5e on page 90.

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




Re: Newbie question about variables, arrays and where they all go

2009-01-18 Thread Telemachus
On Sun Jan 18 2009 @ 10:59, dolphin_sonar wrote:
1 # When calling 'running_sum(5, 6);' the variable 'state @numbers'
 receives those two
2 # parameters, (5 and 6), right? Then, the @numbers array also
 copies/stores (5 and 6)
3 # into the special '( @_ )' variable as well, right? Also, line
 13 pushes '$number' into
4 # the '@numbers' array each time through the foreach loop,
 right?

Maybe I'm not understaning what you mean, but I think you're confused. The
arguments to a subroutine go into the @_ array. The @numbers array is
empty until you load it up in the foreach loop. Having 5 and 6 as arguments
does not automatically put those items into @numbers, nor does @numbers
copy anything into @_.

Also, I understand that you may just be testing out persistent variables in
5.10, but this program is confusing.

Consider what happens if you call running sum a second time (say as
running_sum( 2, 7 );) If I add that call, here's my output:

telemachus ~ $ perl sum
The sum of (5 6) is 11
The sum of (5 6 2 7) is 31

That appears to say that perl has added 5, 6, 2, and 7 up to 31. What
actually happened was that you added 5, 6, 2 and 7 (20) to 11 (the sum from
the previous call to running_sum - which was saved). To put this another way
around: maybe you want to keep a running sum, but the print statement in the
subroutine is very confusing.

Hope this helps, T

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




Re: Newbie question about variables, arrays and where they all go

2009-01-18 Thread Telemachus
From: Telemachus telemac...@arpinum.org
Date: Sun, 18 Jan 2009 20:17:27 -0500
To: beginners@perl.org
Subject: Re: Newbie question about variables, arrays and where they all go

On Sun Jan 18 2009 @  7:54, Telemachus wrote:
 The arguments to a subroutine go into the @_ array. The @numbers array is
 empty until you load it up in the foreach loop. 

Edit: I should have said that the @numbers array is empty before the *first*
run. Since it's persistent, after that it already contains all the items from
previous calls. So on a second call, it already has 5 and 6. (I should also
consider spelling 'understand' with a d.)

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




Re: Newbie: Simple conditional on regexp match

2008-08-08 Thread Kenneth Brun Nielsen
On 6 Aug., 16:45, [EMAIL PROTECTED] (Yitzle) wrote:
 On Wed, Aug 6, 2008 at 4:29 AM, Kenneth Brun Nielsen

 [EMAIL PROTECTED] wrote:
  Within a perl program, I want to go to a particular mode when a
  keyword is found. The keyword is a regexp.

  E.g.
  #!/usr/bin/perl -w
  open FILEHANDLE, soatest.soa;
  while (FILEHANDLE){
     if (/^\*| XI/) {
         print match in line: $.\n;
     }
  }

  This conditional seems to be true for all lines (although they don't
  match /^\*| XI/). What is the correct syntax?

  Best regards,
  Kenneth

 It helps if you can provide sample data that you can replicate the issue with.


Thanks, Yitzle. In the mean time, I found out that the problem was the
regex. I forgot to escape the pipe.

/Kenneth


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Newbie - Simple conditional on regexp match

2008-08-08 Thread Kenneth Brun Nielsen
On 6 Aug., 14:05, [EMAIL PROTECTED] (Rob Coops) wrote:
 You are almost right, since it prints all lines all lines match.

 Could you explain what it is that you are trying to match, that way people
 might be able to help getting the regex right.

i forgot about this thread, since it took so long time before I could
see it - sorry!

Anyway, in the meantime I located the problem, and: yes, you are right
that the problem was the regex.

I forgot to escape the pipe (|), which were intended to be the
explicit character.

Thanks anyway.

/Kenneth


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Newbie - Simple conditional on regexp match

2008-08-06 Thread Rob Coops
You are almost right, since it prints all lines all lines match.

Could you explain what it is that you are trying to match, that way people
might be able to help getting the regex right.

The way you are looking at the file line by line is valid and if your regex
matches only a few lines it will only pring those few lines.

On Wed, Aug 6, 2008 at 11:18 AM, Kenneth Brun Nielsen 
[EMAIL PROTECTED] wrote:

 I need to make a conditional on a regular expression match. How can I
 do that?

 E.g. in the code below, it prints all lines, and NOT only the ones
 that match. How can I make the boolean test correct?

 #!/usr/bin/perl -w
 open FILEHANDLE, soatest.soa;
 while (FILEHANDLE){
if (/^\*| XI/) {
print match in line: $.\n;
}
 }


 --
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 http://learn.perl.org/





Re: Newbie - Simple conditional on regexp match

2008-08-06 Thread Rob Dixon
Kenneth Brun Nielsen wrote:

 I need to make a conditional on a regular expression match. How can I
 do that?
 
 E.g. in the code below, it prints all lines, and NOT only the ones
 that match. How can I make the boolean test correct?
 
 #!/usr/bin/perl -w
 open FILEHANDLE, soatest.soa;
 while (FILEHANDLE){
 if (/^\*| XI/) {
   print match in line: $.\n;
 }
 }

The regular expression /^\*| XI/ matches strings that either start with an
asterisk or contain ' XI' anywhere. If you want to match strings that start with
either an asterisk or ' XI' then you need

  /^(?:\*| XI)/

It looks messy, but (?: ... ) is simply non-capturing parentheses. You could use

  /^\*|^ XI/

instead if you prefer.

If you wanted something different from that you must let us know.

HTH,

Rob

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Newbie: Simple conditional on regexp match

2008-08-06 Thread yitzle
On Wed, Aug 6, 2008 at 4:29 AM, Kenneth Brun Nielsen
[EMAIL PROTECTED] wrote:
 Within a perl program, I want to go to a particular mode when a
 keyword is found. The keyword is a regexp.

 E.g.
 #!/usr/bin/perl -w
 open FILEHANDLE, soatest.soa;
 while (FILEHANDLE){
if (/^\*| XI/) {
print match in line: $.\n;
}
 }

 This conditional seems to be true for all lines (although they don't
 match /^\*| XI/). What is the correct syntax?

 Best regards,
 Kenneth



It helps if you can provide sample data that you can replicate the issue with.

It seems to work for me.

__CODE__
#! /usr/bin/perl

# Always...
use warnings;
use strict;

#!/usr/bin/perl -w
while (DATA){
if ( /^\*| XI/ ) {
print match in line: $.\n;
}
}

__DATA__
1
2
* LINE
4
5 XI
6

__OUTPUT__
match in line: 3
match in line: 5

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: newbie question - handling email addresses

2008-05-14 Thread Gunnar Hjalmarsson

Munzilla wrote:

i was told that Perl doesn't like the @ character


Then you were told wrong.

What you need to take into account is that the '@' character in a double 
quoted string needs to be escaped, or else Perl tries to interpret it as 
the start of an array variable.


As regards data from a form submission, the '@' character does not need 
any special treatment.


--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: newbie question - handling email addresses

2008-05-14 Thread Munzilla
Great, thanks.  I had hoped that was the case.

On May 14, 11:33 am, [EMAIL PROTECTED] (Gunnar Hjalmarsson) wrote:
 Munzilla wrote:
  i was told that Perl doesn't like the @ character

 Then you were told wrong.

 What you need to take into account is that the '@' character in a double
 quoted string needs to be escaped, or else Perl tries to interpret it as
 the start of an array variable.

 As regards data from a form submission, the '@' character does not need
 any special treatment.

 --
 Gunnar Hjalmarsson
 Email:http://www.gunnar.cc/cgi-bin/contact.pl


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Newbie File Question....

2008-02-13 Thread Rob Coops
On a unix/linux file system you see the following:

$ ls -la
total 244
drwx--9 rcoops   ddao24096 Feb 13 09:40 .
drwxr-xr-x   39 root root 4096 Feb 11 13:23 ..
-rw---1 rcoops   ddao2  158310 Feb 13 09:40 .bash_history
-rw-r--r--1 rcoops   ddao2  24 Apr 25  2006 .bash_logout
-rw-r--r--1 rcoops   ddao21228 Sep 12 12:08 .bash_profile
-rw-r--r--1 rcoops   ddao2 169 Nov 20 17:12 .bashrc

On a windows file system you will see:

H:\dir
 Volume in drive H is rcoops$
 Volume Serial Number is 2B01-1BEE
 Directory of H:\
02/05/2008  04:38 PMDIR  Desktop
01/30/2008  01:49 PMDIR  My Documents
02/08/2008  10:28 AMDIR  WINDOWS
12/04/2006  05:32 PMDIR  Program Files

On both file systems (on the command line) if you want to go one directory
up you type: cd .. so the ../file.txt means from the current working
directory (usually where your perl script is started from) go one directory
up and access file.txt

As for the ./file.txt this means as much as: in the current directory a file
called file.txt if this is not found an error is thrown.

If you type just file.txt your OS will look in the working directory and
then look in the directories specified in the PATH environment variable.

So it does nothing more then indicate to your OS which directory it needs to
look. (I am sure there is a far more extensive and more precise way of
explaining this but this is the simplest to understand and correct for most
intents and purposes)

I hope this helps a little bit.

Rob


On Feb 13, 2008 5:34 PM, [EMAIL PROTECTED] wrote:

 I've been going over some listings and I found code

 like the following:



 ./directory/file.txt and

 ../directory/file.txt



 but I've never seen the ./ and ../ things at the

 beginning of the path. I've tried to google these

 but had no luck. I've googled file specification and

 directory specification and got a lot of hits on stuff

 but nothing on what those symbols mean. So, I thought

 I'd ask hereso if you wouldn't mind explaining what

 they are to a newbie I'd appreciate it very much. Thank

 you.




 Portions of this message may be confidential under an exemption to Ohio's
 public records law or under a legal privilege. If you have received this
 message in error or due to an unauthorized transmission or interception,
 please delete all copies from your system without disclosing, copying, or
 transmitting this message.



Re: Newbie File Question....

2008-02-13 Thread Kashif Salman
On Feb 13, 2008 8:34 AM,  [EMAIL PROTECTED] wrote:
 I've been going over some listings and I found code

 like the following:



 ./directory/file.txt and

 ../directory/file.txt



 but I've never seen the ./ and ../ things at the

 beginning of the path. I've tried to google these

 but had no luck. I've googled file specification and

 directory specification and got a lot of hits on stuff

 but nothing on what those symbols mean. So, I thought

 I'd ask hereso if you wouldn't mind explaining what

 they are to a newbie I'd appreciate it very much. Thank

 you.



That has less to do with perl than with the OS and how you navigate
through directories. ./ refers to the current directory you are in
so if you type ./file it will look for file in your current
location. ./directory/file.txt refers to file.txt under director
under your current location. Similarly ../ refers to parent directory.
See http://en.wikipedia.org/wiki/Path_%28computing%29 for more info.
I'd suggest looking up unix/linux basics on google.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Newbie File Question....

2008-02-13 Thread Michael Barnes
[EMAIL PROTECTED] told me on 02/13/2008 10:34 AM:
 I've been going over some listings and I found code
 
 like the following:
 
  
 
 ./directory/file.txt and
 
 ../directory/file.txt
 
  
 
 but I've never seen the ./ and ../ things at the
 
 beginning of the path. I've tried to google these
 
 but had no luck. I've googled file specification and
 
 directory specification and got a lot of hits on stuff
 
 but nothing on what those symbols mean. So, I thought
 
 I'd ask hereso if you wouldn't mind explaining what
 
 they are to a newbie I'd appreciate it very much. Thank
 
 you.
 
  


./ refers to the directory you are in.
../ refers to the parent to the directory you are in, or the one right
above you.

Assume you are in  /home/rcopits and you had several directories, this
that, other.  The full paths would be
/home/rcopits/this
/home/rcopits/that
/home/rcopits/other

If you were in the 'other' directory and you wanted to run a script you
just wrote, you could use ./script.sh instead of
/home/rcopits/other/script.sh

If you wanted to run a script in this directory without moving, you
could use ../this/script2.sh instead of having to type
/home/rcopits/this/script2.sh

I hope that makes sense.

Michael




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Newbie File Question....

2008-02-13 Thread MK

On 02/13/2008 11:34:56 AM, [EMAIL PROTECTED] wrote:
- I've been going over some listings and I found code
-
- like the following:
-
-
-
- ./directory/file.txt and
-
- ../directory/file.txt

this is not really a perl question, but since perl does respect the  
reference:


. and .. are relative directories.  They ALSO have a real  
(absolute) name.


. refers to the directory in question, eg. your present working  
directory (pwd). So if you cd to /home/mine, . now refers to  
/home/mine (that's the real name).


.. refers to the directory above . in a hierarchy.  In /home/mine  
.. would refer to /home.


You really need to find a simple explanation of the unix filesystem, of  
which i am sure there are many on the web; otherwise you are going to  
have have more problems.  Also try:


www.ss64.com/bash/

which explains shell commands like ls and cd, since those commands do  
not have their own manual pages.



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Newbie File Question....

2008-02-13 Thread David Moreno
On Feb 13, 2008 12:30 PM, [EMAIL PROTECTED] wrote:

 Thank you to all who replied to this requestnow I
 understand! I appreciate the help and the courtesy
 of the replies! Once againThank You!!


One more satisfied customer -said somewhere sometime by merlyn.

-- 
David Moreno - http://www.damog.net/
Yes, you can.


RE: Newbie File Question....

2008-02-13 Thread Richard.Copits
Thank you to all who replied to this requestnow I
understand! I appreciate the help and the courtesy
of the replies! Once againThank You!!

-Original Message-
From: Copits Dick 
Sent: Wednesday, February 13, 2008 11:35 AM
To: beginners@perl.org
Subject: Newbie File Question

I've been going over some listings and I found code

like the following:

 

./directory/file.txt and

../directory/file.txt

 

but I've never seen the ./ and ../ things at the

beginning of the path. I've tried to google these

but had no luck. I've googled file specification and

directory specification and got a lot of hits on stuff

but nothing on what those symbols mean. So, I thought

I'd ask hereso if you wouldn't mind explaining what

they are to a newbie I'd appreciate it very much. Thank

you.

 


Portions of this message may be confidential under an exemption to
Ohio's public records law or under a legal privilege. If you have
received this message in error or due to an unauthorized transmission or
interception, please delete all copies from your system without
disclosing, copying, or transmitting this message.

Portions of this message may be confidential under an exemption to Ohio's 
public records law or under a legal privilege. If you have received this 
message in error or due to an unauthorized transmission or interception, please 
delete all copies from your system without disclosing, copying, or transmitting 
this message.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Newbie: Has anyone used Tie::Hash::TwoWay

2008-02-04 Thread axtens
On Feb 4, 12:44 pm, [EMAIL PROTECTED] (John W. Krahn) wrote:
 axtenswrote:
  On Feb 1, 10:00 am, [EMAIL PROTECTED] (Rob Dixon) wrote:
  axtenswrote:
  G'day everyone

John

As it happens, there are commas in the data, and they can be on either
side of the the tab. What's more, I wanted to be able to handle the
possibility of comma and comma+space.

I certainly didn't think of the [] notation, which is a good idea. (Am
showing my beginner status quite well here.)

As for the /e with the FS, the FS is actually a constant for chr(28)
which we're using internally as ... can you believe it ... a field
separator. (How many years has it been in the low end of the ASCII
sequence, never being used as what it was originally designed for?)

Anyway, thanks muchly for your input.

Kind regards,
Bruce.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Newbie: Has anyone used Tie::Hash::TwoWay

2008-02-04 Thread John W. Krahn

John W. Krahn wrote:

axtens wrote:

On Feb 4, 12:44 pm, [EMAIL PROTECTED] (John W. Krahn) wrote:

axtens wrote:


$res =~ s/\^/FS/ge;


The /e option evaluates the FS string as perl code but it is not perl
code so why use the /e option?


As for the /e with the FS, the FS is actually a constant for chr(28)
which we're using internally as ... can you believe it ... a field
separator.


According to my ascii man page:

man ascii
[ SNIP ]
   034   281CFS  (file separator)

You probably want RS:

   036   301ERS  (record separator)



(How many years has it been in the low end of the ASCII
sequence, never being used as what it was originally designed for?)


If FS is actually code then you should show it as code:

 $res =~ s/\^/ FS() /ge;

Or better would be to just use a scalar:

my $FS = \x1C;

...

 $res =~ s/\^/$FS/g;

But then why not just use FS() in the first place:

foreach $l ( @leftList ) {
  foreach $r ( @rightList ) {
$misDict{ !-$l } .= $r . FS();
$misDict{ ?-$r } .= $l . FS();
  }
}


Or you could just use Perl's $; variable which is already set to that value.



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.-- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Newbie: Has anyone used Tie::Hash::TwoWay

2008-02-04 Thread John W. Krahn

axtens wrote:

On Feb 4, 12:44 pm, [EMAIL PROTECTED] (John W. Krahn) wrote:

axtens wrote:


$res =~ s/\^/FS/ge;


The /e option evaluates the FS string as perl code but it is not perl
code so why use the /e option?


As for the /e with the FS, the FS is actually a constant for chr(28)
which we're using internally as ... can you believe it ... a field
separator.


According to my ascii man page:

man ascii
[ SNIP ]
   034   281CFS  (file separator)

You probably want RS:

   036   301ERS  (record separator)



(How many years has it been in the low end of the ASCII
sequence, never being used as what it was originally designed for?)


If FS is actually code then you should show it as code:

 $res =~ s/\^/ FS() /ge;

Or better would be to just use a scalar:

my $FS = \x1C;

...

 $res =~ s/\^/$FS/g;

But then why not just use FS() in the first place:

foreach $l ( @leftList ) {
  foreach $r ( @rightList ) {
$misDict{ !-$l } .= $r . FS();
$misDict{ ?-$r } .= $l . FS();
  }
}



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.-- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Newbie: Has anyone used Tie::Hash::TwoWay

2008-02-04 Thread axtens
On Feb 4, 9:11 pm, [EMAIL PROTECTED] (John W. Krahn) wrote:
 John W. Krahn wrote:
 axtenswrote:
  On Feb 4, 12:44 pm, [EMAIL PROTECTED] (John W. Krahn) wrote:

John,

You're blowing me away with all this kindness.

Thanks.
Bruce.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Newbie: Has anyone used Tie::Hash::TwoWay

2008-02-03 Thread axtens
On Feb 1, 10:00 am, [EMAIL PROTECTED] (Rob Dixon) wrote:
 axtenswrote:
  G'day everyone

Thanks for that. I did, however, give up on using TwoWay and used an
idea a colleague had given me, as below:

sub Misspellings_Setup {
  @wordsList = split /\n/, '__WORDLIST__'
abandonned  abandoned
aberation   aberration
abilities   abilties
.
.
.
__WORDLIST__
;

  foreach (@wordsList) {
($left, $right) = split /\t/, $_;
@leftList = split /,\s* /, $left;
@rightList = split /,\s*/, $right;
foreach $l (@leftList) {
  foreach $r (@rightList) {
  if ( exists $misDict{!-$l} ) { $misDict{!-$l} .= $r . ^; }
else { $misDict{!-$l} = $r . ^; }
  if ( exists $misDict{?-$r} ) { $misDict{?-$r} .= $l . ^; }
else { $misDict{?-$r} = $l . ^; }
  }
}
  }
}

misDict is an our declared elswhere. Getting something out of the
hash is as below.

sub Misspellings_Suggest {
#receives string
#returns string
my $res;
my $string = shift;
if ( ! %misDict ) {
Misspellings_Setup();
}
if ( exists( $misDict{!-$string} ) ) {
$res = $misDict{!-$string};
} else {
$res = $string;
}
$res =~ s/\^/FS/ge;
return $res;
}

Kind regards,
Bruce.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Newbie: Has anyone used Tie::Hash::TwoWay

2008-02-03 Thread John W. Krahn

axtens wrote:

On Feb 1, 10:00 am, [EMAIL PROTECTED] (Rob Dixon) wrote:

axtenswrote:

G'day everyone


Thanks for that. I did, however, give up on using TwoWay and used an
idea a colleague had given me, as below:

sub Misspellings_Setup {
  @wordsList = split /\n/, '__WORDLIST__'
abandonned  abandoned
aberation   aberration
abilities   abilties
.
.
.
__WORDLIST__
;


Why not just create @wordsList as an AoA so you don't have to do all 
that splitting:


my @wordsList = (
[ 'abandonned', 'abandoned' ],
[ 'aberation',  'aberration' ],
[ 'abilities',  'abilties' ],
...
);



  foreach (@wordsList) {
($left, $right) = split /\t/, $_;
@leftList = split /,\s* /, $left;
@rightList = split /,\s*/, $right;


I didn't see any commas in your data so what is this supposed to be doing?



foreach $l (@leftList) {
  foreach $r (@rightList) {
  if ( exists $misDict{!-$l} ) { $misDict{!-$l} .= $r . ^; }
else { $misDict{!-$l} = $r . ^; }
  if ( exists $misDict{?-$r} ) { $misDict{?-$r} .= $l . ^; }
else { $misDict{?-$r} = $l . ^; }


You don't have to test for the key existence as perl will autovivify the 
values.




  }
}
  }
}

misDict is an our declared elswhere. Getting something out of the
hash is as below.

sub Misspellings_Suggest {
#receives string
#returns string
my $res;
my $string = shift;
if ( ! %misDict ) {
Misspellings_Setup();
}
if ( exists( $misDict{!-$string} ) ) {
$res = $misDict{!-$string};
} else {
$res = $string;
}
$res =~ s/\^/FS/ge;


The /e option evaluates the FS string as perl code but it is not perl 
code so why use the /e option?




return $res;
}



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.-- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Newbie: Has anyone used Tie::Hash::TwoWay

2008-01-31 Thread Rob Dixon

axtens wrote:

G'day everyone

I'm confused. I'm trying to make use of Tie::Hash::TwoWay to give me
access to a dictionary of word = misspelling. Has anyone got any
idea how I would use TwoWay for this? Nothing I do seems to work.

  my $secondary = $dict-{0};
  while ( ($k, $v) = ( each %$secondary ) ) {
  print $k . \t . $v . \n;
  }

  my $primary = $dict-{1};
  while ( ($k, $v) = ( each %$primary ) ) {
  print $k . \t . $v . \n;
  }


Your code looks ok. What is it doing wrong? Or what isn't it doing that
it should be? Can you give us an example of your data please?

I can't see how you're expecting to use this module for mispellings. If
you use a properly-spelled word as the hash key it will return a list of
misspellings. If you use a misspelled word as a key it will offer you a
list of corresponding properly-spelled ones. Is that what you want?

Rob

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Newbie: Has anyone used Tie::Hash::TwoWay

2008-01-31 Thread Rob Dixon

axtens wrote:

G'day everyone

I'm confused. I'm trying to make use of Tie::Hash::TwoWay to give me
access to a dictionary of word = misspelling. Has anyone got any
idea how I would use TwoWay for this? Nothing I do seems to work.

  my $secondary = $dict-{0};
  while ( ($k, $v) = ( each %$secondary ) ) {
  print $k . \t . $v . \n;
  }

  my $primary = $dict-{1};
  while ( ($k, $v) = ( each %$primary ) ) {
  print $k . \t . $v . \n;
  }


After sending my previous post it occurs to me that you may not realise
that the values of the tied hash are themselves hash references. Try a
loop like this:

  while (my ($k, $v) = each %$secondary) {
printf %s = (%s)\n, $k, join ' ', keys %$v;
  }

is that closer to what you expect?

Oh, and please

  use strict;
  use warnings;

at the start of your code. Thanks.

Rob



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Newbie question on substitution with Subroutines

2007-10-25 Thread Paul Lalli
On Oct 25, 12:01 pm, [EMAIL PROTECTED] (Minky Arora) wrote:
 I need to make multiple substitutions in a file.There could be a
 situation where there are more than one substitution on a single
 line.I want to count the total # of substituitions.I need to use
 subroutines:

Says who?  Is this a homework assignment?  If so why aren't you asking
your teacher for help?

 I have 3 questions:

 1) Do I have to make 3 separate Regex.

It depends on what you are substituting and replacing.  If you're
searching for three different things and replacing them all with the
same thing, then no.  Just use the | alternation regexp character.

 How can it be done in a single line?

Why do you want it to be?

 2)How to use Subroutines with this?  If I just want the Sub to
 substitute the value and return the line?

The same way you use subroutines in any other way.  What is your
question here?  Do you not know how to write a subroutine?  Do you not
know how to call a subroutine?

Have you read `perldoc perlsub` yet?

 3)Is it a good practice to open the file using Sub? If so, how to do that?

This question is nonsensical.   Try again.

 !/usr/bin/perl -w

You forgot the # in front of the shebang.  This code does not compile.

 use strict;
 my $line;
 my ($count,$count1,$count3,$count2);

 open FILE, data.txt or diecnt open $!;
foreach $LINE(FILE) {

Don't use foreach() to process a file line-by-line.  Use while().


 if ($var =~ s/cellomics/array/g) {
 $count1++;
   }

This counts the number of lines in which this substitution was made.
It does not count the number of substitutions that were made.  That
is, if this line contained two instances of cellomics that were both
replaced with array, $count1 would only go up by one, not by two.  If
that's what you want, fine.  If not, change to:

$count1 += ($var =~ s/cellomics/array/g);

   if  ($var=~ s/perkin almer/janus/g) {
 $count2++
 }
   if  ($var =~ s/dell/laptop/g)  {
 $count3++
 }}

Same comments here.

Paul Lalli


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Newbie question on substitution with Subroutines

2007-10-25 Thread Dr.Ruud
minky arora schreef:

 my ($count,$count1,$count3,$count2);

Consider: my @count;

-- 
Affijn, Ruud

Gewoon is een tijger.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Newbie

2006-10-15 Thread Darwin Pintado
Thanks Romeo for the heads up!

--- Romeo Theriault [EMAIL PROTECTED] wrote:

 Perl is cross-platform, extremely useful for working
 with large  
 amounts of text, using regular expressions, it has a
 great repository  
 of modules for you to use for just about any task
 you can think of  
 (CPAN), it's open source, it's got a great
 community, the list goes  
 on and on.
 
 I would recommend picking up a book to begin your
 way in perl.  
 Learning Perl by Randal Schwartz  Tom Phoenix is a
 great starting  
 place.
 
 Romeo
 
 
 
 On Oct 10, 2006, at 10:55 PM, Darwin Pintado wrote:
 
  Hi David,
 
  I fresh beginner in perl and I'd like to know how
 what
  started you in doing perl. What are other benefits
 of
  learninng perl and what use will it be in the
 future.
 
  Can you also provide other learning sites.
 
  Thank you in advance for your help!
 
 
  __
  Do You Yahoo!?
  Tired of spam?  Yahoo! Mail has the best spam
 protection around
  http://mail.yahoo.com
 
  -- 
  To unsubscribe, e-mail:
 [EMAIL PROTECTED]
  For additional commands, e-mail:
 [EMAIL PROTECTED]
  http://learn.perl.org/
 http://learn.perl.org/first-response
 
 
 
 


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Newbie

2006-10-13 Thread Romeo Theriault
Perl is cross-platform, extremely useful for working with large  
amounts of text, using regular expressions, it has a great repository  
of modules for you to use for just about any task you can think of  
(CPAN), it's open source, it's got a great community, the list goes  
on and on.


I would recommend picking up a book to begin your way in perl.  
Learning Perl by Randal Schwartz  Tom Phoenix is a great starting  
place.


Romeo



On Oct 10, 2006, at 10:55 PM, Darwin Pintado wrote:


Hi David,

I fresh beginner in perl and I'd like to know how what
started you in doing perl. What are other benefits of
learninng perl and what use will it be in the future.

Can you also provide other learning sites.

Thank you in advance for your help!


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




RE: Newbie Question

2006-09-20 Thread M K Scott
Hi,
 
I have tried that to no avail.  I have also tried a simple match of !~ 
m/(m|f){1}/ and even put in the code you suggested to read !~ m/^(m|f){1}$/ but 
this still doesn't work properly.   Input of d or T will work to say it is 
incorrect and input of m or f will be accepted but I was under the 
impression that the {1} would limit it to only accept a single character while 
in practice it still accepts ff and mmm.   Any ideas?
 
One further question though, an example question I am doing asks for a text 
file to be read in and the number of digits to be counted (ie 3 1's, 6 2's 
etc) and I can read input in and do a 'getc' and pattern match that against a 
hash containing word references to the numbers and then add one to a count but 
is this the best way to do this?
 
Thanks again,
Mark



From: John W. Krahn [mailto:[EMAIL PROTECTED]
Sent: Wed 20/09/2006 01:12
To: Perl Beginners
Subject: Re: Newbie Question



M K Scott wrote:
 Hi all,

Hello,

 Please forgive the newbie nature of this question but i'm just
 starting off teaching myself perl from scratch and so need a little
 clarification.
 
 I am trying to put together a script to do pattern matching and while
 I can get the basic syntax alright it doesn't seem to be working as
 expected.   For one example I need to match a user input and make sure
 it is a certain length (i.e. doesn't exceed a certain number of
 characters) and this is the code so far:
 
 while ($string != m/[a-zA-Z]{1,5}/ )
 { print(that is wrongtry again: );
chomp ($string = STDIN);   }

You need to anchor the pattern like /^[a-zA-Z]{1,5}$/


John
--
use Perl;
program
fulfillment

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




RE: Newbie Question

2006-09-20 Thread Lee Goddard
use strict;
use warnings;

my $string;
while (1){
print Please enter 1-5 letters: ;
chomp ($string = STDIN);
last if $string =~ m/^[a-z]{1,5}$/i;
# last if $string =~ m/^\w{1,5}$/;
print that is wrongtry again: ;
}

I think the problem was not the pattern so much as the rest of the
logic.

The pattern you have will match a lower- or upper-case letter of the
alphabet one or five times. I can't see how anchoring it to the start or
end of the $string would help anything other than perhaps internal
efficiency, for which I'd anchor it at both ends (^ and $ for start and
end). I would suggest using the /i modifier, so instead of /[a-zA-Z]/ to
match any letter, you could /[a-z]/i. 

But that's not really 'any letter' -- it's just a to z, which may ignore
accented and other UTF-8 characters. You can find better options in
perldoc perlre -- see esp. the POSIX section.

Your original code also seemed to be warning of an error before taking
input, but I guess that's a cut-n-paste issue.

Hth
lee

 From: M K Scott [mailto:[EMAIL PROTECTED] 

 I have tried that to no avail.  I have also tried a simple 
 match of !~ m/(m|f){1}/ and even put in the code you 
 suggested to read !~ m/^(m|f){1}$/ but this still doesn't 
 work properly.   Input of d or T will work to say it is 
 incorrect and input of m or f will be accepted but I was 
 under the impression that the {1} would limit it to only 
 accept a single character while in practice it still accepts 
 ff and mmm.   Any ideas?
  
 One further question though, an example question I am doing 
 asks for a text file to be read in and the number of digits 
 to be counted (ie 3 1's, 6 2's etc) and I can read input 
 in and do a 'getc' and pattern match that against a hash 
 containing word references to the numbers and then add one to 
 a count but is this the best way to do this?
  
 Thanks again,
 Mark
 
 
 
 From: John W. Krahn [mailto:[EMAIL PROTECTED]
 Sent: Wed 20/09/2006 01:12
 To: Perl Beginners
 Subject: Re: Newbie Question
 
 
 
 M K Scott wrote:
  Hi all,
 
 Hello,
 
  Please forgive the newbie nature of this question but i'm just 
  starting off teaching myself perl from scratch and so need a little 
  clarification.
  
  I am trying to put together a script to do pattern matching 
 and while 
  I can get the basic syntax alright it doesn't seem to be working as
  expected.   For one example I need to match a user input 
 and make sure
  it is a certain length (i.e. doesn't exceed a certain number of
  characters) and this is the code so far:
  
  while ($string != m/[a-zA-Z]{1,5}/ )
  { print(that is wrongtry again: );
 chomp ($string = STDIN);   }
 
 You need to anchor the pattern like /^[a-zA-Z]{1,5}$/

-- 

http://www.bbc.co.uk/
This e-mail (and any attachments) is confidential and may contain personal 
views which are not the views of the BBC unless specifically stated.
If you have received it in error, please delete it from your system.
Do not use, copy or disclose the information in any way nor act in reliance on 
it and notify the sender immediately.
Please note that the BBC monitors e-mails sent or received.
Further communication will signify your consent to this.


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Newbie Question

2006-09-20 Thread John W. Krahn
M K Scott wrote:
 Hi,

Hello,

 I have tried that to no avail.  I have also tried a simple match
 of !~ m/(m|f){1}/ and even put in the code you suggested to read
 !~ m/^(m|f){1}$/ but this still doesn't work properly.   Input of
 d or T will work to say it is incorrect and input of m or
 f will be accepted but I was under the impression that the {1}
 would limit it to only accept a single character while in practice
 it still accepts ff and mmm.   Any ideas?

The expression above seems to work for me:

$ perl -le'
for ( qw/ a aa aaa f ff fff m mm mmm / ) {
print $_: , !/^(m|f){1}$/ ? NOT  : , found;
}
'
a: NOT found
aa: NOT found
aaa: NOT found
f: found
ff: NOT found
fff: NOT found
m: found
mm: NOT found
mmm: NOT found

Note that /^(m|f){1}$/ could also be written as /^[mf]$/.


 One further question though, an example question I am doing asks
 for a text file to be read in and the number of digits to be
 counted (ie 3 1's, 6 2's etc) and I can read input in and do
 a 'getc' and pattern match that against a hash containing word
 references to the numbers and then add one to a count but is
 this the best way to do this?

Using getc() is usually not the best way to do anything in Perl.




John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




RE: Newbie Question

2006-09-20 Thread M K Scott
Thanks to all who replied - it is working fine now and I am now moving on to 
the next problemall your help was appreciated and very usefull.  I am sure 
I will be back here soon.
 
Thanks
Mark



From: John W. Krahn [mailto:[EMAIL PROTECTED]
Sent: Wed 20/09/2006 17:07
To: Perl Beginners
Subject: Re: Newbie Question



M K Scott wrote:
 Hi,

Hello,

 I have tried that to no avail.  I have also tried a simple match
 of !~ m/(m|f){1}/ and even put in the code you suggested to read
 !~ m/^(m|f){1}$/ but this still doesn't work properly.   Input of
 d or T will work to say it is incorrect and input of m or
 f will be accepted but I was under the impression that the {1}
 would limit it to only accept a single character while in practice
 it still accepts ff and mmm.   Any ideas?

The expression above seems to work for me:

$ perl -le'
for ( qw/ a aa aaa f ff fff m mm mmm / ) {
print $_: , !/^(m|f){1}$/ ? NOT  : , found;
}
'
a: NOT found
aa: NOT found
aaa: NOT found
f: found
ff: NOT found
fff: NOT found
m: found
mm: NOT found
mmm: NOT found

Note that /^(m|f){1}$/ could also be written as /^[mf]$/.


 One further question though, an example question I am doing asks
 for a text file to be read in and the number of digits to be
 counted (ie 3 1's, 6 2's etc) and I can read input in and do
 a 'getc' and pattern match that against a hash containing word
 references to the numbers and then add one to a count but is
 this the best way to do this?

Using getc() is usually not the best way to do anything in Perl.




John
--
use Perl;
program
fulfillment

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Newbie Question

2006-09-19 Thread Igor Sutton


while ($string != m/[a-zA-Z]{1,5}/ )
{ print(that is wrongtry again: );
   chomp ($string = STDIN);   }



Maybe:

while ($string !~ m/.../) {
...
}

When you're matching against regular expressions, you need to use =~ or !~.

Hope this helps.

--
Igor Sutton Lopes
t: +55 51 9627.0779
e: [EMAIL PROTECTED]


Re: Newbie Question

2006-09-19 Thread John W. Krahn
M K Scott wrote:
 Hi all,

Hello,

 Please forgive the newbie nature of this question but i'm just
 starting off teaching myself perl from scratch and so need a little
 clarification.
  
 I am trying to put together a script to do pattern matching and while
 I can get the basic syntax alright it doesn't seem to be working as
 expected.   For one example I need to match a user input and make sure
 it is a certain length (i.e. doesn't exceed a certain number of
 characters) and this is the code so far:
  
 while ($string != m/[a-zA-Z]{1,5}/ ) 
 { print(that is wrongtry again: ); 
chomp ($string = STDIN);   }

You need to anchor the pattern like /^[a-zA-Z]{1,5}$/


John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Newbie Perl Question

2006-07-17 Thread Dr.Ruud
Rod Burgess schreef:

Your Subject has no quality. Please come up with something like convert
data lines.

 I am new to the Perl world and am trying to learn it.  A coworker
 tells me that Perl will not work for what I am trying to do however,
 I think Perl would be a great tool to use and  I feel this coworker
 is wrong.
 I have a file that contains several lines all as below:
 DR03555{tab}45600062888{tab} 8FLAT WASHER
 DR03555{tab}228765329{tab}1GASKET

 The meaning of the file is
 DR03555 = order number
 45600062888 = part number
 8 = quantity
 FLAT WASHER = Description

 The lines all begin with the prefex DR  I would like to read this
 file and produce the following output:

 45600062888;8;FLAT WASHER
 228765329;1;GASKET

 basiclly I need a file that lists the following:
 Part#;Quantity;Description

 Is this possible with Perl?

Certainly.

#!/usr/bin/perl
# ID: pqd.pl
  use warnings ;
  use strict ;

  while (  )
  {
/^DR\d+.*?(\d+).*?(\d+)(.+)/
  and print $1;, 0+$2, ;$3\n ;
  }

If your data is in pqd.in, and pqd.pl is execuable, then just run it as

  pqd.pl pqd.in

-- 
Affijn, Ruud

Gewoon is een tijger.



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Newbie Perl Question

2006-07-17 Thread Todd W

Rod Burgess [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
I am new to the Perl world and am trying to learn it.  A coworker tells me
 that Perl will not work for what I am trying to do however, I think Perl
 would be a great tool to use and  I feel this coworker is wrong.
 I have a file that contains several lines all as below:
 DR03555{tab}45600062888{tab} 8FLAT WASHER
 DR03555{tab}228765329{tab}1GASKET

 The meaning of the file is
 DR03555 = order number
 45600062888 = part number
 8 = quantity
 FLAT WASHER = Description

 The lines all begin with the prefex DR  I would like to read this file and
 produce the following output:

 45600062888;8;FLAT WASHER
 228765329;1;GASKET

 basiclly I need a file that lists the following: 
 Part#;Quantity;Description

 Is this possible with Perl?


You bet. This is what perl was made for:

$ cat tab2csv.dat
DR03555 45600062888 8FLAT WASHER
DR03555 228765329   1GASKET

$ cat tab2csv.pl
while (my $line = ) {
  my( $order, $part, $qty, $descr ) = $line =~ m|(.+?)\t(.+?)\t(\d+)(.+)$|;
  print( join(';', $part, $qty + 0, $descr), \n );
}

$ perl tab2csv.pl tab2csv.dat
45600062888;8;FLAT WASHER
228765329;1;GASKET

Enjoy,

Todd W.



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Newbie Perl Question

2006-07-17 Thread Maxim
Hello Rod,

I am not the author of the initial message, but thanks a lot for your
reply.
I am trying to adopt your script to my need, and, basically, it works
for me.

But I am totally frustrated in regexp part in the script:
/^DR\d+.*?(\d+).*?(\d+)(.+)/

Could you please explain it? Or maybe you have a link to intelligible
guide for perl regexp?

I am sure a lot of the readers of this list would happy to understand
this really great part of perl :)


On Mon, Jul 17, 2006 at 08:13:59PM +0200, Dr.Ruud wrote:
 Rod Burgess schreef:
 
 Your Subject has no quality. Please come up with something like convert
 data lines.
 
  I am new to the Perl world and am trying to learn it.  A coworker
  tells me that Perl will not work for what I am trying to do however,
  I think Perl would be a great tool to use and  I feel this coworker
  is wrong.
  I have a file that contains several lines all as below:
  DR03555{tab}45600062888{tab} 8FLAT WASHER
  DR03555{tab}228765329{tab}1GASKET
 
  The meaning of the file is
  DR03555 = order number
  45600062888 = part number
  8 = quantity
  FLAT WASHER = Description
 
  The lines all begin with the prefex DR  I would like to read this
  file and produce the following output:
 
  45600062888;8;FLAT WASHER
  228765329;1;GASKET
 
  basiclly I need a file that lists the following:
  Part#;Quantity;Description
 
  Is this possible with Perl?
 
 Certainly.
 
 #!/usr/bin/perl
 # ID: pqd.pl
   use warnings ;
   use strict ;
 
   while (  )
   {
 /^DR\d+.*?(\d+).*?(\d+)(.+)/
   and print $1;, 0+$2, ;$3\n ;
   }
 
 If your data is in pqd.in, and pqd.pl is execuable, then just run it as
 
   pqd.pl pqd.in
 
 -- 
 Affijn, Ruud
 
 Gewoon is een tijger.
 
 
 
 -- 
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 http://learn.perl.org/ http://learn.perl.org/first-response
 

-- 
Maxim 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Newbie Perl Question

2006-07-17 Thread Rob Dixon

Rod Burgess wrote:

 I am new to the Perl world and am trying to learn it.  A coworker tells me
 that Perl will not work for what I am trying to do however, I think Perl
 would be a great tool to use and  I feel this coworker is wrong.
 I have a file that contains several lines all as below:
 DR03555{tab}45600062888{tab} 8FLAT WASHER
 DR03555{tab}228765329{tab}1GASKET

 The meaning of the file is
 DR03555 = order number
 45600062888 = part number
 8 = quantity
 FLAT WASHER = Description

 The lines all begin with the prefex DR  I would like to read this file and
 produce the following output:

 45600062888;8;FLAT WASHER
 228765329;1;GASKET

 basiclly I need a file that lists the following:
 Part#;Quantity;Description

 Is this possible with Perl?

It certainly is. Your colleague is very misguided: this is just the sort of
thing Perl was originally designed for. I have set up a text file with the
contents you describe and the following program produces the results you asked
for:

  use strict;
  use warnings;

  open my $fh, 'file' or die $!;

  while ($fh) {
if (/^(DR\d+)\s+(\d+)\s+(\d{5})(.+?)\s*$/) {
  my ($ordnum, $part, $qty, $desc) = ($1, $2, $3, $4);
  $qty += 0;
  print $part;$qty;$desc\n;
}
  }

  close $fh;

OUTPUT

  45600062888;8;FLAT WASHER
  228765329;1;GASKET

The while loop reads and processes each record in the data file. If the line
matches the regular expression (which also extracts the data fields if it is
successful) then the fields are reformatted and printed. The regular expression
is looking for a line that starts with 'DR' followed by at least one digit and
some whitespace; a sequence of one or more digits and some more whitespace; five
digits and then any text to the end of the line, excluding trailing whitespace.
Those four fields go into $1 .. $4 and are copied to named variables. Adding
zero to $qty changes it from a string '8' to a number 8 to remove the
irrrelevant leading zeroes.

I hope that's clear enough for you.

Rob

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Newbie Perl Question

2006-07-17 Thread Peter Hoose
I'm pretty new myself, as such I like to see a little more error checking to 
accomodate for mine (or other people's) mistakes, here's one similar to 
Rob's but with some additional error checking that you might want, basically 
the main difference is that it expects your lines to be formatted like:


dr5digitsTABVariableLengthFieldOfDigitsTAB5digitsFreeFormDescription

* upper or lower case is fine thanks to the i switch at the end of the regex

If the line doesn't look like that it will let you know what line is in 
error and exit.


This also prints both to the screen and to an output file:

Screen Output:

print Writing output to: $OutputFileName: 
$PartNum;$Quantity;$Description\n;


File Output:


print OUTF $PartNum;$Quantity;$Description\n;


I find this to be quite handy when I'm first running a script like this as 
it will help me see how things are working real time.


If you want to stop printing to either one, just delete the line or comment 
it out with a preceding #.


===BEGIN COPY PASTE SCRIPT=

#!/usr/bin/perl

use strict;
use warnings;

my $InputFileName   = somefile.txt;
my $OutputFileName  = outputfile.txt;

open(INF, $InputFileName) or die Could not open $InputFileName for reading: 
$!\n;
open(OUTF, $OutputFileName) or die Could not open $OutputFileName for 
output: $!\n;


print \n;

while(INF) {
 if ($_ !~ /^(dr\d{5})(\t)(\d*)(\t)(\d{5})(.*)/i) {
   print Found an incorrectly formatting line at line: $. $_, please 
correct this line and try again\n;

   exit;
 }
 else {
   my ($OrderNum, $PartNum, $Quantity, $Description) = ($1, $3, $5, $6);
   $Quantity += 0;
   chomp($Description);
   print Writing output to: $OutputFileName: 
$PartNum;$Quantity;$Description\n;

   print OUTF $PartNum;$Quantity;$Description\n;
 }
}

print \n\nAll done! Please see: $OutputFileName for your data!\n;

close(INF);
close(OUTF);

END COPY PASTE SCRIPT

Hope this helps!

~P


- Original Message - 
From: Rod Burgess [EMAIL PROTECTED]

To: beginners@perl.org
Sent: Monday, July 17, 2006 1:59 PM
Subject: Newbie Perl Question



I am new to the Perl world and am trying to learn it.  A coworker tells me
that Perl will not work for what I am trying to do however, I think Perl
would be a great tool to use and  I feel this coworker is wrong.
I have a file that contains several lines all as below:
DR03555{tab}45600062888{tab} 8FLAT WASHER
DR03555{tab}228765329{tab}1GASKET

The meaning of the file is
DR03555 = order number
45600062888 = part number
8 = quantity
FLAT WASHER = Description

The lines all begin with the prefex DR  I would like to read this file and
produce the following output:

45600062888;8;FLAT WASHER
228765329;1;GASKET

basiclly I need a file that lists the following: 
Part#;Quantity;Description


Is this possible with Perl?



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response








--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Newbie Perl Question

2006-07-17 Thread Peter Cornelius


On Jul 17, 2006, at 11:33 AM, Maxim wrote:



But I am totally frustrated in regexp part in the script:
/^DR\d+.*?(\d+).*?(\d+)(.+)/

Could you please explain it? Or maybe you have a link to intelligible
guide for perl regexp?



perldoc perlre is a pretty detailed description of perl's regexp.  If  
you want to really get into it you can install YAPE::Regex::Explain  
and see how it parses everything out.

 http://search.cpan.org/~pinyan/YAPE-Regex-Explain-3.011/

The specific answer is:

/^DR\d+.*? #^ means start of the line, DR is a literal 'D' followed  
by an 'R',
 # \d is the digit character set, + means one or  
more of the preceding,

 # so one or more digits.
 # The '.' is any character, '*' is zero or more  
times and '?' is non-greedy,
 #which means the match should stop as soon as  
possible,

 #ie, before blasting past the next match

(\d+).*?   #parens mean 'capture' so this is capture one or more  
digits.
#Then there's that non-greedy, match anything  
construct again


(\d+)(.+) #This is the same as above except instead of the non- 
greedy thing
   #it captures the description string with the 'one  
or more of anything'

   #construct.
/x  #I added the 'x' on here because it lets you put comments into a  
regex like I did above.


Hope this helps,

Peter Cornelius
Sr. Software Engineer
LiveOps (http://liveops.com)



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: newbie sort question

2006-04-03 Thread Val Genova
thanks

this solved my problem

my @output_sorted = sort { (split /,/, $b)[0] =  (split /,/, $a)[0] }
@output;

thanks to all that helped
Jeff Pang [EMAIL PROTECTED] writes:

this is my friend's script

# collect all score
  my @output = ();
  my @old_output = ();
  foreach my $list (@bugkillers) {
my ($id,$name) = split(/,/, $list);
my $score =
$Bugs-getSCORE($showold,$id,$contest,$pContest,$groups);
push(@output,$score,$id,$name);
  }
  # print result
  foreach my $result (sort {$b = $a} @output) {
my ($score,$id,$name) = split(/,/, $result);
$html.=HTMLcode;


Hi,you have some mistakes when sorting the array.Because your array
@output include the mixed elements,not just the numbers,so you get the
bad result when sorting them with '='.

I would suggest you change the style of @output as:

put @output,[$score,$id,$name];

Now you can sort them by the array's NO.1 element (which is score) via
accessing array's ref:

for (sort {$b-[0] = $a-[0]} @output){
  ...
}


Hope it helps and warning for no test.




--
Jeff Pang
NetEase AntiSpam Team
http://corp.netease.com



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: newbie sort question

2006-03-23 Thread Jeff Pang

this is my friend's script

# collect all score
  my @output = ();
  my @old_output = ();
  foreach my $list (@bugkillers) {
my ($id,$name) = split(/,/, $list);
my $score =
$Bugs-getSCORE($showold,$id,$contest,$pContest,$groups);
push(@output,$score,$id,$name);
  }
  # print result
  foreach my $result (sort {$b = $a} @output) {
my ($score,$id,$name) = split(/,/, $result);
$html.=HTMLcode;


Hi,you have some mistakes when sorting the array.Because your array @output 
include the mixed elements,not just the numbers,so you get the bad result when 
sorting them with '='.

I would suggest you change the style of @output as:

put @output,[$score,$id,$name];

Now you can sort them by the array's NO.1 element (which is score) via 
accessing array's ref:

for (sort {$b-[0] = $a-[0]} @output){
  ...
}


Hope it helps and warning for no test.




--
Jeff Pang
NetEase AntiSpam Team
http://corp.netease.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: newbie needs help on hidden and CGI parameters

2006-03-09 Thread Sean Davis
You realize that hidden fields are not hidden, right?  You just have to
view the source of the page in which they are embedded and you now know the
username and password, and that with every round-trip to the server, this
information is sent in plain-text (unless you are using SSL)?  I would read
up on using sessions to store these types of sensitive information.

As for why you can login with empty username and password, is it possible
that your mysql allows that from localhost?  You may want to check on that.

Sean

On 3/9/06 11:41 AM, Mary Anderson [EMAIL PROTECTED] wrote:

 
 Hi all,
I have a login screen login.pl which calls another application
 sampleEntry.pl.  The login and password are passed to a multipage Sample
 Entry program as CGI parameters
 
sampleEntry.pl?login=myloginpassword=mypassword.
 
 These two parameters are used in one of the pages to login to a MySQL
 database, and are passed back to sampleEntry.pl when that program is
 reloaded to create a new record.
 
Hidden, which I am trying to use to pass these two parameters around, is
 a complete mystery!
 
My previously reported performance degradation apparently came from
 misusing hidden. In those days, there was just one application. Once I
 learned I could use it to pass parameters to fields which appeared on one
 page and not the other, I used it liberally throughout the program.  It
 appeared to have some very strange effects -- namely doubling the number of
 fields each time it was used on a page on which the fields appeared!  This
 wrecked havoc with my performance.
 
 Now, however, it is a complete mystery.  I call sampleEntry with the
 login and password, and work my way through the application until I get to
 the page which connects with the database.  At that point, it connects even
 though I do 
 
 my $login = param(-name='Login');
 my $password = param(-name='Password');
 print login = $login,
hr;
 print password = $password,
 hr;
 
 $DBH = DBI-connect(dbi:mysql:meadowRue:localhost, $login, $password);
 
 
 just before doing the database connect.
 
 Output;  login =
  password =
 
 But the database connects
 
 Then, when I try to pass the value of login back to sampleEntry, which is
 being reloaded to start a new record,  the value of $login is seen to in
 fact be null.
 
 How should I be passing these parameters?  SampleEntry has multiple pages
 but no explicit Login or Password textfields.
 
 Thanks
 
 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: newbie question about print header

2006-03-08 Thread Bill Stephenson

From the CGI.pm docs...

Most browsers will not cache the output from CGI scripts. Every time 
the browser reloads the page, the script is invoked anew. You can 
change this behavior with the -expires parameter. When you specify an 
absolute or relative expiration interval with this parameter, some 
browsers and proxy servers will cache the script's output until the 
indicated expiration date. The following forms are all valid for the 
-expires field:


+30s  30 seconds from now
+10m  ten minutes from now
+1h   one hour from now
-1d   yesterday (i.e. ASAP!)
now   immediately
+3M   in three months
+10y  in ten years time
Thursday, 25-Apr-1999 00:40:33 GMT  at the indicated time  date

So, you'd want this...


print $Q-header(-expires='-1d');


Kindest Regards,

--
Bill Stephenson


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: newbie help for podcast script [with attach]

2005-07-31 Thread John Doe
[...]
 The script works fine for a normal pod feed. It only fails when the feed
 only has one channel-item.

 Is line 65 OK?

 foreach my $item (@{$data-{channel}-{item}}) {

 all I want to do is step through one-by-one each item. Does that code
 work properly if there is only one item instead of many?

To find out, just make test cases:

use strict; use warnings;
my $data={channel={item=[qw/one two/]}}; # 1 items 
foreach my $item (@{$data-{channel}-{item}}) {
print $item\n;
}
print \n;
$data={channel={item=[qw/one/]}}; # 1 item
foreach my $item (@{$data-{channel}-{item}}) {
print $item\n;
}

# output:
one
two

one

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: newbie help for podcast script [with attach]

2005-07-31 Thread alexeijh
On Sat, 2005-07-30 at 20:15 +1000, [EMAIL PROTECTED] wrote:
 On Fri, 2005-07-29 at 19:03 -0700, John W. Krahn wrote:
 
  You are trying to call the get() method on the $feed object.  Read the
  documentation for the correct syntax for XML::Simple::XMLin().
  
  perldoc XML::Simple
  
  
  As to the warning message:
  
  perldoc perldiag
  [snip]
 Odd number of elements in anonymous hash
 (W misc) You specified an odd number of elements to
 initialize a hash, which is odd, because hashes come in
 key/value pairs.
  
  So your $data variable is not being assigned a valid anonymous hash
  probably because you are using XML::Simple::XMLin() incorrectly.
  
 
 Thanks for the advice.
 I'm not sure that that is the issue. 
 
 I experimented with 1st downloading the file and using XMLin('foo.xml')
 and the behaviour was the same.
 
 The script works fine for a normal pod feed. It only fails when the feed
 only has one channel-item.
 
 Is line 65 OK?
 
 foreach my $item (@{$data-{channel}-{item}}) {
 
 all I want to do is step through one-by-one each item. Does that code
 work properly if there is only one item instead of many?

OK, the answer to the above question is no, if there is only one item in
the xml file, then the Perl script will fail at this point:

Not an ARRAY reference at ./foo.pl line XX.

Any tips on the correct way to do what I am attempting.

 
 
 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




RE: newbie help for podcast script [with attach]

2005-07-31 Thread Charles K. Clarkson
[EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote:

: This is basically my 1st perl script.
:
: It's a podcast download app. I don't know the etiquette for
: attaching files, but I have attached a 100 line perl file.
:
: It seems that it fails if there is only 1 podcast for a feed
: with:
:
: fetching http://www.abc.net.au/science/k2/podcast/drk_rss.xml ...
:
: Odd number of elements in anonymous hash
: at /home/lexhider/bin/GodCast.pl line 65.
: fileparse(): need a valid pathname at
: /home/lexhider/bin/GodCast.pl line 68


The XML::Simple parser will not assign a single item
(item/item) to an array reference like it does with many
items (unless you specify it to do so). If $data-{channel}-{item}
is an array reference, you have many items. If it is a hash
reference you have only one item. If the item key does not
exist, then there are no items.

if ( exists $data-{channel}-{item} ) {
# 1 or more items.

} else {
# No items.
}

We can check the number of items using the perl ref() function.

if ( exists $data-{channel}-{item} ) {

# 1 or more items.
if ( ref $data-{channel}-{item} eq 'HASH' ) {
# 1 item

} elsif ( ref $data-{channel}-{item} eq 'ARRAY' ) {
# More than 1 item.
}

} else {
# No items.
}

Ideally, we would like XML::Simple to return the following.

 - No items An array with no items in it.
 - One item An array with only one item in it.
 - Many items   An array with many items in it.

if ( exists $data-{channel}-{item} ) {

# 1 or more items.
if ( ref $data-{channel}-{item} eq 'HASH' ) {
$data-{channel}-{item} = [ $data-{channel}-{item} ];
# 1 item

} elsif ( ref $data-{channel}-{item} eq 'ARRAY' ) {
# more than 1 item.
}

} else {
# No items.
$data-{channel}-{item} = [];
}

Since the many items section doesn't do anything (it's in
the form we want), we can eliminate that choice.

if ( exists $data-{channel}-{item} ) {

if ( ref $data-{channel}-{item} eq 'HASH' ) {
# Only 1 item.
$data-{channel}-{item} = [ $data-{channel}-{item} ];
}

} else {
# No items.
$data-{channel}-{item} = [];
}


As it turns out, XML::Simple has an option to force arrays
when there is only one item. Now we only need to check on zero
items (though that probably shouldn't happen).

my $xml = XML::Simple-new();
my $source  = get $feed or die qq(Failed to fetch feed: $feed.)
my $data= $xml-XMLin( $source, ForceArray = [ 'item' ] );

# Handle zero items.
$data-{channel}-{item} = [] unless exists $data-{channel}-{item};


Now that we always have an item array we can tackle the
$latest option. While we can do the testing as you did, it might
be easier if we just limit the size of the item array. Then we can
step through the array without a test each time. Here's a way to
do it using the min() function from List::Util.

use List::Util qw( min );

# Get just the array items we want to process.
if ( $latest ) {
my $item_count =
min( $latest, scalar @{ $data-{channel}-{item} } );

@{ $data-{channel}-{item} } =
@{ $data-{channel}-{item} }[0 .. $item_count - 1];
}

And then the loop.

foreach my $item ( @{ $data-{channel}-{item} } ) {

my $cast = $item-{enclosure}-{url};
chomp( my $base = basename($cast) );

if ( ! -e $feed_dir/$base ) {
`wget -c $cast`;
move( $base, $feed_dir );

} else {
print On HD: $base.\n;
}
}

Putting that together we get something like this.
(Not tested.)

use strict;
use warnings;
use Getopt::Long;
use XML::Simple;

# Imported functions
use File::Basename  qw( basename );
use File::Copy; qw( move );
use List::Util  qw( min );
use LWP::Simple qw( get );

my $download_dir = $ENV{HOME}/Audio/Podcasts;
my $config_dir   = $ENV{HOME}/.GodCast;

# File with url of feeds on each line.
my $feeds = $config_dir/feeds.txt;

# Change tmp if you want semi-downloaded
# files to stay after a reboot.
my $tmp = /tmp/Podcasts;

my $latest = 0;
my $across; # Not implemented.

GetOptions(
'latest|l=s' = \$latest,
'across' = \$across
);

foreach my $dir ( $download_dir, $config_dir, $tmp, ) {
unless( -d $dir ) {
mkdir $dir or die qq(Failed to make dir $dir: $!);
}
}

die qq(File $feeds not readable: $!) unless -r $feeds;

open FEEDS, $feeds or die qq(Could not open file: $feeds: $!);

while ( my $feed = FEEDS ) {
chomp $feed;
print \nfetching $feed ... \n\n;

my $xml = XML::Simple-new();
my $source  = get $feed or die qq(Failed to fetch feed: $feed.);
my $data= $xml-XMLin( $source, ForceArray = [ 'item' ] );

# Handle zero items.
$data-{channel}-{item} = [] unless exists $data-{channel}-{item};


my $feed_dir= $download_dir/$data-{channel}-{title};
my $temp_feed_dir   = $tmp/$data-{channel}-{title};

foreach my $dir ( $feed_dir, $temp_feed_dir ) {
unless (-d $dir) {
  

Re: newbie help for podcast script [with attach]

2005-07-30 Thread alexeijh
On Fri, 2005-07-29 at 19:03 -0700, John W. Krahn wrote:

 You are trying to call the get() method on the $feed object.  Read the
 documentation for the correct syntax for XML::Simple::XMLin().
 
 perldoc XML::Simple
 
 
 As to the warning message:
 
 perldoc perldiag
 [snip]
Odd number of elements in anonymous hash
(W misc) You specified an odd number of elements to
initialize a hash, which is odd, because hashes come in
key/value pairs.
 
 So your $data variable is not being assigned a valid anonymous hash
 probably because you are using XML::Simple::XMLin() incorrectly.
 

Thanks for the advice.
I'm not sure that that is the issue. 

I experimented with 1st downloading the file and using XMLin('foo.xml')
and the behaviour was the same.

The script works fine for a normal pod feed. It only fails when the feed
only has one channel-item.

Is line 65 OK?

foreach my $item (@{$data-{channel}-{item}}) {

all I want to do is step through one-by-one each item. Does that code
work properly if there is only one item instead of many?



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: newbie help for podcast script [with attach]

2005-07-29 Thread John W. Krahn
[EMAIL PROTECTED] wrote:
 *** SORRY, forgot to attach file. ***
 
 Hi,

Hello,

 This is basically my 1st perl script.
 
 It's a podcast download app. I don't now the etiquette for attaching
 files, but I have attached a 100 line perl file.
 
 It seems that it fails if there is only 1 podcast for a feed with:
 
 fetching http://www.abc.net.au/science/k2/podcast/drk_rss.xml ...
 
 Odd number of elements in anonymous hash
 at /home/lexhider/bin/GodCast.pl line 65.

It looks like you are using XML::Simple::XMLin() incorrectly:

 51 my $data = $xml-XMLin(get $feed or die failed to fetch
$feed\n);


$ perl -MO=Deparse,-p -e'my $data = $xml-XMLin(get $feed or die failed
to fetch $feed\n);'
(my $data = $xml-XMLin(($feed-get || die(failed to fetch $feed\n;
-e syntax OK

You are trying to call the get() method on the $feed object.  Read the
documentation for the correct syntax for XML::Simple::XMLin().

perldoc XML::Simple


As to the warning message:

perldoc perldiag
[snip]
   Odd number of elements in anonymous hash
   (W misc) You specified an odd number of elements to
   initialize a hash, which is odd, because hashes come in
   key/value pairs.

So your $data variable is not being assigned a valid anonymous hash
probably because you are using XML::Simple::XMLin() incorrectly.


 fileparse(): need a valid pathname at /home/lexhider/bin/GodCast.pl line
 68

You are using File::Basename::basename() to parse a URL.  You should use
a module that understands URLs to do that.


John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




  1   2   3   4   5   >