Fw: Hash/Associtative Array

2003-03-20 Thread Horace Franklin Jr.



I am writing a CGI Script like the guestbook.cgi script.
 
I want following actions to occur:
a. Menu item calls my script (cgi).
b. Script displays a menu with four items on it.
c. Menu items calls form (sub routine) i.e.,
 sub entry_form {  
my $q    = shift;  my $url = 
"">
  
  my $form = 
<   
Hello!       
My name is:     
My E-mail is:     
Message:    
    
Type your message here.    
       
 E_FORM
 
   $form;}d. I want to use my %input vice my $q.
e. I want the hash receive the input from the form.
f. I do not know if the syntax shown in example in 'c' above
   will work or do I need something like:
   print ""$input{'name'} \"    size=15 
maxlength=20>"\n;
 
In other words, I want a self contained script (the script 
displays forms, process form info, and sends mail, etc). 
 
see attached file guestbook.cgi 
 
Horace
- Original Message - 
From: "Hanson, Rob" <[EMAIL PROTECTED]>
To: "'Horace Franklin Jr.'" <[EMAIL PROTECTED]>
Sent: Thursday, March 20, 2003 5:22 PM
Subject: RE: Hash/Associtative Array
> I'm confused by your example.  If you want to recieve 
input from a form it> would look like this...> > use CGI 
':standard';> > my $fname = param('fname');> > The 
"use CGI" part is a bunch of libraries to help in CGI scripting.> 
Checkboxes are the same, except that param('chkbox') won't be defined if 
it> wasn't checked.> > Was that your question?> 
> Rob> > > -Original Message-> From: 
Horace Franklin Jr. [mailto:[EMAIL PROTECTED]> Sent: Thursday, March 
20, 2003 6:22 PM> To: Hanson, Rob> Subject: Hash/Associtative 
Array> > > Sir,> > Please accept my apology 
for contacting you directly.  I am desperate.> Please let me 
explain, I have been reading several perl books since I> last asked a 
question about perl: CG I Script [guestbook.cgi]> > I am fascinate 
with the capabilities of the Hash/Associtative array:> my %array  I 
have also learned about my and local.> > I want to take advantage 
of the assocititave array. How can I> accomplish the following?> 
> Use a Hash/Accositative array to recevie input from a form in a perl 
script:> Is think this is the correct syntax to accomplish the desired 
effect but I> am> not certian.>  print "> size=15 
maxlength=20>"\n;> > What is the syntax if I wanted receive 
input from a checkbox?> Is it different?> > Any assistance 
will be greatly appreciated.> > Horace


guestbook.cgi
Description: Binary data
-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

how to upload image in perl

2003-03-20 Thread Dave

Hai ,

I have a problem uploading images with perl. I have HTML code within perl. When i just 
link the image useing the link tag 



It doesnt work. I tried giving the path ../search.gif. Is there any other way to 
upload the image.

Thanks in advance,




-
With Yahoo! Mail you can get a bigger mailbox -- choose a size that fits your needs


My own #@$%%# fault (was: "new" confusion)

2003-03-20 Thread Rob Richardson
Greetings!

As always happens, it turns out that this whole conundrum was my own
fault.  There were not one but two pieces of glaring idiocy.  First, it
turned out that the mysterious Trains.pm file did indeed have two
separate methods named new().  Second, I followed up on the suggestion
a couple of people made that $trainName seemed not to have anything in
it.  Before ScheduleDay::AddTrain() was called, a line was read from a
.csv file.  split() was used to break the data into separate elements
of an array named @data.  Then, when I called AddTrain(), I passed it
@_   Whatever was in the default array at the time, it wasn't what
I wanted.

Excuse me while I go crawl under a rock and hide.

RobR


__
Do you Yahoo!?
Yahoo! Platinum - Watch CBS' NCAA March Madness, live on your desktop!
http://platinum.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Trapping whitespaces between fields in an array

2003-03-20 Thread Rob Dixon
Gufranul Haque wrote:
> Hello all,
>
> I need to process a record
>
> Key1   3.00 4.005.00
>
> I need to trap spaces between nonwhitespace characters as array
> elements i.e for the above record
> the resulting array should be
> ('   ',' ','')
>
> The staement that I am using
>
> my @spaces = map /(\s+)/, $_;
>
> is able to trap only the 3 whitespaces between Key1 and 3.00 ('   ')

You're using 'map' to map a single element list ($_), so
your statement is the same as:

my @spaces = $_ =~/(\s+)/;
or just
my @spaces = /\s+/;

You can make this statement return _all_ of the space sequences
by using the /g modifier.

my @spaces = /\s+/g;

but I suspect that you may want all of the non-space data as well.

my @everything = split /(\s+)/;

will return all the data and all of the spaces:

print join ", ", map "'$_'", @everything;
output
'Key1', '   ', '3.00', ' ', '4.00', '', '5.00'

HTH,

Rob




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Class using a class: "new" confusion and strange print behavior

2003-03-20 Thread Rob Dixon
R. Joseph Newton wrote:
>
> Got it!  The changes shown below seem to provide the protection I
> want.  I guess the object hash can be left as a programmer
> scratchpad, while the payload remains protected as a private variable

No, this is a step backwards I'm afraid!

> > package Person;
> >
> > use strict;
> > use warnings;
> > use Address;
> >
> > my ($surname, $given_name, $address);

These are class data, which is fine in itself, but they apply to all
instances of the class, and changing one object's address will change
them all. They are, after all, just

$Person::surname
$Person::given_name
$Person::address

Each object's private data has to be in a separate anonymously
referenced data structure. Don't forget that you can have a
reference to an array or even a scalar if those structures are
more appropriate to your problem.

> > sub new {
> >   my $self = {};
> >   set_name(@_);

Best to call $self->set_name(@_) unless you particularly want to
avoid inheriting the method.

> >   bless $self;
> > }
> >
> > sub set_name {
> >   my $self = shift;
> >   ($surname, $given_name) = @_;

As I said, private data must be within the referenced data
structure.

@{$self}{qw(surname givenname)} = @_;

> > }

The rest, as far as I can see, has only this one misconception.

> > sub print {
> >   my $self = shift;
> >   print "$surname, ";
> >   print "$given_name\n";
> >   return unless defined $address;
> >   $address->print();
> > }
> >
> > sub set_address {
> >   my $self = shift;
> >   $address = Address::new(@_);
> > }
> >
> > my $motto = "Every man, woman and child is a star";
> > __END__

[snip output]

Cheers Joseph




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Class using a class: "new" confusion and strange print behavior

2003-03-20 Thread Rob Dixon
Hi Joseph.

Well, it's 01:00 here and I really should go to bed, but I'll take a
stab at this before I go.

R. Joseph Newton wrote:
> >
> > Well, you goaded me into it.  I finally decided to pull the spoon
> > off my tongue and try Perl OOP.  I must say, it is pretty painless.
> > I can get used to the C-style arrows, though I'd prefer to use a
> > dot operator as in C++, and the bless() function is only mildly
> > fetishistic.  All in all, object creation is pretty
> > straightforward, though.
> >
> > But...
> >
> > I can do this:
> >
> > #!/usr/bin/perl -w
> >
> > use strict;
> >
> > use Person;

Note that the Person.pm file has to be in one of the directories
in @INC for this to work.

> > my $person = Person::new("Newton", "R. Joseph");

No. You should be able to write:

my $person = Person->new("Newton", "R. Joseph");

which will call

my $person = Person::new('Person', "Newton", "R. Joseph");

so you need a slight change to your 'new' method.

> > $person->set_address("666 NoSpam way", "Eugene", "OR", "97402",
> >  "USA");
> > $person->{'Surname'} = "Johnson"; #this
> > $person->print_name();
> > $person->{'What the heck'} = "Jeez, this is a porous guy!";  # and this
> > print "$person->{'What the heck'}\n";
> >
> > Without anything preventing me.

Yes. But you won't know what's in the hash that forms the object
unless you look through the code. Encapsulation says that your
only knowledge of an API should be its external interface.

> > I am not at all sure that this is a good thing.

It is. A very good thing. It means you can do what you need
to without anything to stop you, but you have to be quite
deliberate about it if you do. C quite happy for you to stomp
over anything in your virtual address space, and unless you get
an access violation you won't know anything about it until
you start getting strange errors after your software's been
running for some time. As the Camel says:

"...perl isn't obsessed with enforced privacy... a Perl module
would prefer that you stayed out of its living room because
you weren't invited, not because it has a shotgun."

If it were an easy thing to do /accidentally/ then I wouldn't
like it, but as it is I'm happy. A bit like having a 'goto' in case
I need it, but easy to avoid if I want to do things 'properly'.

> > Is there any way to construct a class so that member
> > data stays private?

Yes, using closures, but I wold forget about that now until you
can write a module that holds together.

> > This was the factor that kept me from
> > venturing into Perl OOP, and it still seems like it could lead to
> > problems if the only thing preventing this kind of external access
> > is the injuction "Don't do that!".

I've never seen a library module that enforces privacy, and they
seem to work OK!

> > package Person;
> >
> > use strict;
> > use warnings;
> > use Address;
> >
> > sub new {

It's common to allow all methods to be called as object methods
or class methods. 'new' is a common exception (because it doesn't
read well to write:

my $object2 = new $object1;
rather than
my $object2 = new Class;

the framework:

sub new {
my $self = shift;
my ($surname, $givenname) = @_;
my $class = ref $self || $self;
bless {surname => $surname, givenname => $givenname}, $class;
}

> >   my $self = {
> > 'Surname' => $_[0],
> > 'Given Name' => $_[1]
> >   };
> >   bless $self;

This is OK but won't allow inheritance. You need to bless the
new object into the class it was called with.

> > }
> >
> > sub print_name {
> >   my $self = shift;
> >   print "$self->{'Surname'}, ";
> >   print "$self->{'Given Name'}\n";
> >   return unless defined $self->{'Address'};
> > #  print "$self->{'Address'}->{'Street'}\n";
> > #  print "$self->{'Address'}->{'City'}, ";
> > #  print "$self->{'Address'}->{'State'}  ";
> > #  print "$self->{'Address'}->{'PostCode'}\n";
> > #  print "$self->{'Address'}->{'Country'}\n" if
> > #  defined $self->{'Address'}->{'Country'};
> >   $self->{'Address'}->print();
> > }

Yes. You could tidy up your hash accessing using slices
and 'join' but that's fine.

> > sub set_address {
> >   my $self = shift;
> >   my $address = Address::new(@_);

my $address = new Address (@_);

> > #  $address{'Street'} = $_[0];
> > #  $address{'City'} = $_[1] if defined $_[1];
> > #  $address{'State'} = $_[2] if defined $_[2];
> > #  $address{'PostCode'} = $_[3] if defined $_[3];
> > #  $address{'Country'} = $_[4] if defined $_[4];
> >   $self->{'Address'} = $address;

Well yes, but:


$self->{'Address'} = new Address (@_);

(you could even drop the (@_) at the end, because a
subroutine without a parameter list will be passed @_
by default).

> > }
> >
> > #my $conclusion = "Person has a name";
> > 1;
> > __END__
> >
>
>
>
> > package Address;
> >
> > use strict;
> > use warnings;
> >
> > sub new {
> >   my $self = {
> > 'Street' => $_[0],
> >   };
> >   $self->{'City'} = $_[1] if defined $_[1];
> >   $self->{'State

Filling out forms on the web. (completed).

2003-03-20 Thread Larry Wissink
Hi,
Thanks to everyone for their responses.  I am very impressed by how quickly
people responded.  I'm subscribed to the digest format, so I didn't realize
that my complete script didn't get posted until now.  (I think it was a
paste error on my part.)  So, for completeness, in case anyone wanted to see
it, here it is.

Again, thanks for the suggestions. 

Larry Wissink.

This script retrieves the login page.  I pipe this to a file from the
command line:
c:\do_get.pl > befree_login.html

#perl
use strict;
use LWP;
use HTTP::Cookies;
my $browser;


my $webpage;
$webpage = do_GET ('http://affiliates.befree.com/Affiliates/index.jsp');

print $webpage;



sub do_GET {
$browser = LWP::UserAgent->new() unless $browser;
$browser->agent("Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US;
rv:1.0.2)");
my $cookie_file = "cookie.txt";
$browser->cookie_jar(HTTP::Cookies->new(
'file' => $cookie_file, 'autosave' => 1
));
my @netscape_like_headers = (
 'UserAgent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0.2)',
 'Accept-Language' => 'en-US',
 'Accept-Charset' => 'iso8859-1,*,utf-8',
 'Accept-Encoding' => 'gzip',
 'Accept' => "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
image/png, */*"
 );
my $resp = $browser->get(@_,@netscape_like_headers);
return ($resp->content, $resp->status_line, $resp->is_success, $resp)
  if wantarray;
return unless $resp->is_success;
return $resp->content;
}


This script is supposed to login to the site using the info from the
previous webpage.  Unfortunately, it returns an error message saying the
page is temporarily unavailable.  I think that means that I haven't got the
cookies or authentication quite right.

#perl -w
#be_free_login.pl

use strict;
my $out_file = "befree_login.html";

use LWP;
use HTTP::Cookies;

my $browser = LWP::UserAgent->new;
$browser->agent("Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US;
rv:1.0.2)");
my $cookie_file = 'C:\Documents and
[EMAIL PROTECTED]';
$browser->cookie_jar(HTTP::Cookies->new(
'file' => $cookie_file, 'autosave' => 1
));
my @netscape_like_headers = (
 'UserAgent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0.2)',
 'Accept-Language' => 'en-US',
 'Accept-Charset' => 'iso8859-1,*,utf-8',
 'Accept-Encoding' => 'gzip',
 'Accept' => "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
image/png, */*"
 );

my $response = $browser->post(
 
'http://affiliates.befree.com/Affiliates/login.do;jsessionid=25M6fzYMqVuao0l
mWQV81dAO4JAfEudUHgzqg4n3nwmfzIFWQe9p!1888438520!-1062723984!8001!7002',
  [ 
  "UserAgent" => "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US;
rv:1.0.2)",
 "Accept-Language" => "en-US",
 "Accept-Charset" => "iso8859-1,*,utf-8",
 "Accept-Encoding" => "gzip",
 "Accept" => "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
image/png, */*",
   "username" => "someone",
   "password" => "***",
   "submit" => "Log On",
   "languageId" => "1"
   ]
 );
   
 die "Error: ", $response->status_line, "\n"
   unless $response->is_success;
   
 open(OUT, ">$out_file") || die "Can not write-open $out_file: $!";
 binmode(OUT);
 print OUT $response->content;
 close(OUT);
 print "Bytes saved: ", -s $out_file, " in $out_file\n";
 
 

Original Message:
Hi,
I'm a Perl newbie in way over my head, but I love a challenge.  
My company needs to download transaction data from a partner's website on a
regular basis.  The data can be retrieved through a form, (entering date
ranges, selecting merchants, etc.), but one must log in to the site first
and must use Netscape 4.0 or IE 5.0 or higher.  I am trying to use LWP and
related Perl modules to emulate a Netscape browser, login to the site and
then fill out the form.
Unfortunately, I just don't know enough about emulating a browser or secure
transactions to do the job.  I am hoping someone out there can point me to
some resources for learning more about these topics.  Most of what I have
been able to do comes from reading "Perl & LWP" by Sean M. Burke (O'Reilly
Press).
I include what I have managed to write, drawn heavily from the Burke book,
for the edification (or amusement) of others.  Again, any insight will be
greatly appreciated.

Sincerely,

Larry Wissink
[EMAIL PROTECTED]

This script retrieves the login page.  I pipe this to a file from the
command line:
c:\do_get.pl > befree_login.html

#perl
use strict;
use LWP;
use HTTP::Cookies;
my $browser;



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Multi-OS script

2003-03-20 Thread Wiggins d'Anconia
[EMAIL PROTECTED] wrote:
I can encapsulate the uses in a string eval, but
then it seems I must put all use of that (thoese)
modules into the same string eval, which gets
syntatically sticky quickly.
Can someone point me to a simple, elegant, perlish
solution?
Maybe switching to 'require' instead of 'use' since it is run time 
instead of compile time?  You may want to check out the bowels of:

perldoc -f require

As it describes how to perform a require using, 'eval', etc.

http://danconia.org

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


RE: Trapping whitespaces between fields in an array

2003-03-20 Thread Wagner, David --- Senior Programmer Analyst --- WGO
Gufranul Haque wrote:
> Hello all,
> 
> I need to process a record
> 
> Key1   3.00 4.005.00
> 
> I need to trap spaces between nonwhitespace characters as array
> elements i.e for the above record the resulting array should be
> ('   ',' ','')
> 
> The staement that I am using
> 
> my @spaces = map /(\s+)/, $_;
> 
Just a a g to /(\s+)/g and it will do what you want. You and I think like 
split but you need the g otherwise it will get the first space and quit.  Add the g 
and it works like you expect.
Wags ;)


**
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Class using a class: "new" confusion and strange print behavior

2003-03-20 Thread Paul Johnson
On Thu, Mar 20, 2003 at 03:46:16PM -0800, R. Joseph Newton wrote:
> "R. Joseph Newton" wrote:

[ Perl allows you to manipulate an object other than via its interface ]

> > Without anything preventing me.  I am not at all sure that this is a
> > good thing.  Is there any way to construct a class so that member
> > data stays private?  This was the factor that kept me from venturing
> > into Perl OOP, and it still seems like it could lead to problems if
> > the only thing preventing this kind of external access is the
> > injuction "Don't do that!".
> 
> Got it!  The changes shown below seem to provide the protection I
> want.  I guess the object hash can be left as a programmer scratchpad,
> while the payload remains protected as a private variable

No.  This will work fine if your class is a singleton, but every object
will be accessing the same data.

> > package Person;
> >
> > use strict;
> > use warnings;
> > use Address;
> >
> > my ($surname, $given_name, $address);

This is class data.  There is only one copy of these variables.  Every
object will access the same copy.

> > sub new {
> >   my $self = {};
> >   set_name(@_);
> >   bless $self;
> > }
> >
> > sub set_name {
> >   my $self = shift;
> >   ($surname, $given_name) = @_;
> > }
> >
> > sub print {
> >   my $self = shift;
> >   print "$surname, ";
> >   print "$given_name\n";
> >   return unless defined $address;
> >   $address->print();
> > }
> >
> > sub set_address {
> >   my $self = shift;
> >   $address = Address::new(@_);
> > }
> >
> > my $motto = "Every man, woman and child is a star";
> > __END__

Perl can do what you want, if you want to pay the price.  If you use a
closure as the object instead of a hash, you can ensure that everyone
plays by whatever rules you dictate.  For this, you pay the price of
calling an extra function, plus whatever you do in it.

In general though, Perl programmers are more pragmatic, and take the
approach of documenting the interface and saying "if you don't use the
API you're on your own".

Having said that, Perl 6 will be providing the B&D features you are
looking for.

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Trapping whitespaces between fields in an array

2003-03-20 Thread Gufranul Haque
Hello all,

I need to process a record

Key1   3.00 4.005.00

I need to trap spaces between nonwhitespace characters as array elements i.e for the 
above record
the resulting array should be
('   ',' ','')

The staement that I am using 

my @spaces = map /(\s+)/, $_;

is able to trap only the 3 whitespaces between Key1 and 3.00 ('   ')


Please let me know.

Thanks.



Re: Class using a class: "new" confusion and strange print behavior

2003-03-20 Thread R. Joseph Newton
"R. Joseph Newton" wrote:

>
> But...
>
> I can do this:
>
> #!/usr/bin/perl -w
>
> use strict;
>
> use Person;
>
> my $person = Person::new("Newton", "R. Joseph");
> $person->set_address("666 NoSpam way", "Eugene", "OR", "97402",
>  "USA");
> $person->{'Surname'} = "Johnson"; #this
> $person->print_name();
> $person->{'What the heck'} = "Jeez, this is a porous guy!";  # and this
> print "$person->{'What the heck'}\n";
>
> Without anything preventing me.  I am not at all sure that this is a good thing.  Is 
> there any way to construct a class so that member data stays private?  This was the 
> factor that kept me from venturing into Perl OOP, and it still seems like it could 
> lead to problems if the only thing preventing this kind of external access is the 
> injuction "Don't do that!".
>
> Joseph
>

Got it!  The changes shown below seem to provide the protection I want.  I guess the 
object hash can be left as a programmer scratchpad, while the payload remains 
protected as a private variable

> package Person;
>
> use strict;
> use warnings;
> use Address;
>
> my ($surname, $given_name, $address);
>
> sub new {
>   my $self = {};
>   set_name(@_);
>   bless $self;
> }
>
> sub set_name {
>   my $self = shift;
>   ($surname, $given_name) = @_;
> }
>
> sub print {
>   my $self = shift;
>   print "$surname, ";
>   print "$given_name\n";
>   return unless defined $address;
>   $address->print();
> }
>
> sub set_address {
>   my $self = shift;
>   $address = Address::new(@_);
> }
>
> my $motto = "Every man, woman and child is a star";
> __END__
>

Results before change:

E:\d_drive\perlStuff>PersonTest.pl
Johnson, R. Joseph# Name has been corrupted by wayward code
666 NoSpam way
Eugene, OR  97402
USA
Jeez, this is a porous guy!

After:
Newton, R. Joseph# Still intact
666 NoSpam way
Eugene, OR  97402
USA
Jeez, this is a porous guy!

With an additional line to demonstrate proper modification by object methods:

#!/usr/bin/perl -w

use strict;

use Person;

my $person = Person::new("Newton", "R. Joseph");
$person->set_address("666 NoSpam way", "Eugene", "OR", "97402",
 "USA");
$person->{'Surname'} = "Johnson"; # Still here, but doesn't touuch the payload
$person->set_name("Clinton", "William");  #  Safe modification--uses object method
$person->print();
$person->{'What the heck'} = "Jeez, this is a porous guy!";
print "$person->{'What the heck'}\n";

And the output:

Clinton, William
666 NoSpam way
Eugene, OR  97402
USA
Jeez, this is a porous guy!

I like it.

Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Class using a class: "new" confusion and strange print behavior

2003-03-20 Thread R. Joseph Newton
Rob Dixon wrote:

> ...
> Yes, but it's
>
> $schedule_day_object->AddTrain($train_name);
>
> which does that for you.
>
> ...

> It's a tradition that methods start with:
>
> my $self = shift;
> my ( $trainName, $p1, $p2, $p3, @prest ) = @_;
>
> to separate the implicit parameter from the explcit one.
> But what Rob R wrote will do just as well.

Hi Rob,

Well, you goaded me into it.  I finally decided to pull the spoon off my tongue and 
try Perl OOP.  I must say, it is pretty painless.  I can get used to the C-style 
arrows, though I'd prefer to use a dot operator as in C++, and the bless() function is 
only mildly fetishistic.  All in all, object creation is pretty straightforward, 
though.

But...

I can do this:

#!/usr/bin/perl -w

use strict;

use Person;

my $person = Person::new("Newton", "R. Joseph");
$person->set_address("666 NoSpam way", "Eugene", "OR", "97402",
 "USA");
$person->{'Surname'} = "Johnson"; #this
$person->print_name();
$person->{'What the heck'} = "Jeez, this is a porous guy!";  # and this
print "$person->{'What the heck'}\n";

Without anything preventing me.  I am not at all sure that this is a good thing.  Is 
there any way to construct a class so that member data stays private?  This was the 
factor that kept me from venturing into Perl OOP, and it still seems like it could 
lead to problems if the only thing preventing this kind of external access is the 
injuction "Don't do that!".

Joseph
package Person;

use strict;
use warnings;
use Address;

sub new {
  my $self = {
'Surname' => $_[0],
'Given Name' => $_[1]
  };
  bless $self;
}

sub print_name {
  my $self = shift;
  print "$self->{'Surname'}, ";
  print "$self->{'Given Name'}\n";
  return unless defined $self->{'Address'};
#  print "$self->{'Address'}->{'Street'}\n";
#  print "$self->{'Address'}->{'City'}, ";
#  print "$self->{'Address'}->{'State'}  ";
#  print "$self->{'Address'}->{'PostCode'}\n";
#  print "$self->{'Address'}->{'Country'}\n" if
#  defined $self->{'Address'}->{'Country'};
  $self->{'Address'}->print();
}

sub set_address {
  my $self = shift;
  my $address = Address::new(@_);
#  $address{'Street'} = $_[0];
#  $address{'City'} = $_[1] if defined $_[1];
#  $address{'State'} = $_[2] if defined $_[2];
#  $address{'PostCode'} = $_[3] if defined $_[3];
#  $address{'Country'} = $_[4] if defined $_[4];
  $self->{'Address'} = $address;
}

#my $conclusion = "Person has a name";
1;
__END__  
package Address;

use strict;
use warnings;

sub new {
  my $self = {
'Street' => $_[0],
  };
  $self->{'City'} = $_[1] if defined $_[1];
  $self->{'State'} = $_[2] if defined $_[2];
  $self->{'PostCode'} = $_[3] if defined $_[3];
  $self->{'Country'} = $_[4] if defined $_[4];
  bless $self;
}

sub print {
  my $self = shift;
  print "$self->{'Street'}\n";
  print "$self->{'City'}, ";
  print "$self->{'State'}  ";
  print "$self->{'PostCode'}\n";
  print "$self->{'Country'}\n" if
  defined $self->{'Country'};
}

my $exultation = 'Whee-haw!!  we got adrresses, podner!';
__END__


PersonTest.pl
Description: Perl program
-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Re: Filling out forms on the web.

2003-03-20 Thread Randal L. Schwartz
> "Pete" == Pete Emerson <[EMAIL PROTECTED]> writes:

Pete> Randal Schwartz just wrote an excellent article titled "Screen Scraping
Pete> for Fun and Profit" in Linux Magazine. His code is available at
Pete> http://www.linuxmagazine.com/downloads/2003-04/perl, although I recommend
Pete> reading the article (April 2003 edition), because he walks the code line
Pete> by line and gives you an in depth understanding about what is going on.

I also recommend that you get that from *my* site instead.
.  I provide a search
interface at the bottom of each page to search the more than 160+
articles that I've written for the various magazines.

Pete> Despite the plug, I am not associated with Randal or Linux Magazine in any
Pete> way, other than being a subscriber. :)

Despite the plug, I am. :)

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[EMAIL PROTECTED]> http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Filling out forms on the web.

2003-03-20 Thread Rob Dixon
Hi Larry.

I saw the 'Larry W' and, just for a moment, thought we had
the man himself here!

Larry Wissink wrote:
> Hi,
> I'm a Perl newbie in way over my head, but I love a challenge.
> My company needs to download transaction data from a partner's
> website on a regular basis.  The data can be retrieved through a
> form, (entering date ranges, selecting merchants, etc.), but one must
> log in to the site first and must use Netscape 4.0 or IE 5.0 or
> higher.  I am trying to use LWP and related Perl modules to emulate a
> Netscape browser, login to the site and then fill out the form.
> Unfortunately, I just don't know enough about emulating a browser or
> secure transactions to do the job.  I am hoping someone out there can
> point me to some resources for learning more about these topics.
> Most of what I have been able to do comes from reading "Perl & LWP"
> by Sean M. Burke (O'Reilly Press).
> I include what I have managed to write, drawn heavily from the Burke
> book, for the edification (or amusement) of others.  Again, any
> insight will be greatly appreciated.
>
> Sincerely,
>
> Larry Wissink
> [EMAIL PROTECTED]
>
> This script retrieves the login page.  I pipe this to a file from the
> command line:
> c:\do_get.pl > befree_login.html
>
> #perl
> use strict;
> use LWP;
> use HTTP::Cookies;
> my $browser;

I guess seomthing went wrong in your post? Without knowing exactly
what you need to do I can only gieva  few pointers, all of which will
be in Sean's book anyway.

use LWP;
use HTTP::Request::Common;

will allow you to create a user agent and fetch a URL:

my $ua = new LWP::UserAgent;
my $url = 'http://host.com/path.page.htm';
my $response = $ua->request (GET $url);

use HTML::TreeBuilder;

will allow you to parse HTML. And look for useful bits.

my $tree = HTML::TreeBuilder->new_from_content($response->content);
my @forms = $tree->look_down(_tag => 'form');

use HTTP::Request::Form;

will let you fill in forms and press buttons:

  my $form = HTTP::Request::Form->new ($forms[0], $url);
  $form->field (username -> 'Larry');
  my $response = $ua->request ($form->press('login'));


There's obviously a lot more to it than that, such as
checking response status codes and so on, but it
should give you a pointer. If you need anything
further let us know more about what you're doing
and we'll do our best.

Cheers,

Rob




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Class using a class: "new" confusion and strange print behavior

2003-03-20 Thread Rob Dixon
R. Joseph Newton wrote:
>
> Thanks.  Let me make sure I have this clear, then.  Does any call to
> a class method then have the reference to the object, or the object
> itself, as the first parameter.

Almost. Any method called through an object will have that object
(a blessed reference) as its first parameter. You can also make
'class method' calls, in which case the first parameter will simply be
the name of the module (the package name). Assuming for now
there is no inheritance goin on

my $train = Train->new;
or
my $train = new Train;

(the two are interchangeable except for minor syntactic difficulties)
will both call

Train::new('Train');

whereas

my $vehicle = $train->new;
or
my $vehicle = new $train;

will call

Train::new($train);

> In that case, it certainly does make sense to shift it back out
> before handling the rest.of the arguments.

Every time you're feeling lost, the following (from perldoc
perlobj) will make you feel a little more secure; mainly
through its insistence on using 'simply'!

1.  An object is simply a reference that happens to know which class it
belongs to.

2.  A class is simply a package that happens to provide methods to deal
with object references.

3.  A method is simply a subroutine that expects an object reference (or
a package name, for class methods) as the first argument.

HTH,

Rob




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Class using a class: "new" confusion and strange print behavior

2003-03-20 Thread Rob Dixon
R. Joseph Newton wrote:
>
> Just a hunch, since the material in question is out of reach, but
> could this be a circular reference?  It happens a lot when assembling
> C headers.  If Train.pm by any chance has a "use ScheduleDay;"
> statement, then that would porobably cause some havoc.

In general you don't get the same problems in Perl that you do in
C, as 'require' is smart. If you call

require File;

it first checks $INC{'File.pm'} to see if it has already been included.
If not then it goes ahead and looks through the paths in @INC
to find the module file. When it is found, $INC{'File.pm'} is set
to the module file's absolute path, and  a 'do' performed on it.
Thereafter, if the same 'require' is encountered, the file will
not be read again as its entry in %INC is defined.

Having said this, the initial compilation unit (ScheduleDay.pm)
isn't added to the INC hash, so if it 'require's a module which
then 'require's the original file again it seems to me that the
redefinition warning will be issued.

Even so, all of this is speculative until we can see the real thing,
so in the meantime I'm sticking with my simpler possibility:
that Train.pm doesn't have a 'package Train' statement!

> It might be worth the bandwidth for Rob R. to post all three
> files--the two modules as well as the driver used to test them.

Definitely. Then we can rewrite it for him.

;-)

Rob




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Class using a class: "new" confusion and strange print behavior

2003-03-20 Thread Rob Dixon
[EMAIL PROTECTED] wrote:
>
> I was hoping someone would correct me, if you have? ;-)

Although I could make a correction!

[EMAIL PROTECTED] wrote:
>
> Rob's suggestion that you lack a package statement for Train could be
> it, it could also be that Train exports 'new', which is placing it in
> the current namespace, then you are redefining it. Probably you
> shouldn't export 'new' if it is.

I don't think your option is possible. If Train was adding
ScheduleDay::new to the symbol table, then the message
would be:

Subroutine new redefined at ScheduleDay.pm line 7.

as it would see the definition that Train provided first.

Cheers,

Rob






-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Class using a class: "new" confusion and strange print behavior

2003-03-20 Thread Rob Dixon
[EMAIL PROTECTED] wrote:
> 
> On Thu, 20 Mar 2003 14:06:56 -, "Rob Dixon"
> <[EMAIL PROTECTED]> wrote:
>
> >
> > Wiggins' post got me to think a little more carefully, and it is
> > clear
> > that Train.pm is doing the redefining at its line 106, not
> > ScheduleDay.
> > That means that the definition must already exist when the 'use
> > Train'
> > statement in ScheduleDay is executed, as none of the Train code
> > is executed anywhere else. Since 'use' is an implied BEGIN block I
> > can't see any way this is possible.
> >
> > Can you put me out of my misery Rob, and tell us what's on Train.pm
> > line 106?
> >
>
> I was hoping someone would correct me, if you have? ;-)

No I wasn't. It's just that your post made me go back and think
harder. I had written than it was ScheduleDay::new that was
being redefined, but now I'm pretty sure it will be Train::new,
as the code at Train.pm line 106 was trying to redefine a 'new'
that had already been defined. At this point in the compilation
the ScheduleDay module has done nothing but 'use Train' so I
don't think there can be anything in its symbol table yet.

> After reading Bob's e-mail I paid closer attention to the error
> and thought in terms of compile time rather than runtime...
> But I am a bit confused about the order of compiling, does the
> whole file get compiled, then the module, and then they get
> "linked" or is it sequential so that as soon as a non-previously
> compiled 'use' is reached, then the that is compiled and then
> returns to the original point to finish that file? This latter
> seems to make more sense, but then that doesn't mean it is
> the case. I really ought to read up more on Perl

Neither really. The whole process is an orgy of compilation
and execution, with the compiler calling the interpreter to
execute BEGIN blocks in the middle of the compilation
phase, and the interpreter calling the compiler to process
eval expressions and s///ee constructs.

The compiler will process, in this instance, ScheduleDay.pm
until it comes to 'use Train'. This is a hidden BEGIN block
so as soon as it is compiled it will be executed before the
compiler continues through the file. Execution calls 'require'
(which compiles the Train.pm file) and then the 'import'
function of the Train module which is almost always either
non-existent (if it is an OO module) or inherited from
Exporter::import. Once this is done the compiler continues
where it left of in the ScheduleDay.pm file - immediately
after the 'use' statement.

> I really ought to read up more on Perl internals, but finding
> the time when I want to make my head hurt is difficult at
> best ;-)..

The difficulty is that Perl doesn't follow the pattern of
traditional languages, which are compiled to an link form,
linked to an image and then run. The Perl compiler and
interpreter are part of the same unit, allowing
compile-time execution and run-time compilation. At
least it avoids the need for dynamic linking!

Cheers,

Rob






-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: Class using a class: "new" confusion and strange print behavi or

2003-03-20 Thread Bob Showalter
R. Joseph Newton wrote:
> ...
> Does any call to a class method then have the reference to the object,
> or the object itself, as the first parameter.

An "object" is a reference that has been blessed into a class. In a method
call, the reference is passed as the first element in @_.

So, if $obj is a reference blessed into class "MyClass", then

   $ret = $obj->Method($arg);
   MyClass::Method($obj, $arg);

are similar in that both result in two items being passed to Class::Method.
However, the second call does not do inheritance, while the first does.

All this is detailed in perldoc perlobj.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: caller() problems

2003-03-20 Thread david
Rob Hanson wrote:


> I am guessing the problem is (as stated in the perldoc) that "the
> optimizer might have optimized call frames away before "caller" had a
> chance to get
> the information.".  Furthermore it states that ""caller(N)" might not
> return
> information about the call frame you expect it do, for "N > 1"".  ...But
> in my case "N" never exceeds 1.
> 

actually, the optimizer didn't optimized the frames. the number you pass to 
caller tells Perl how many levels atop of the current call for the info.

for example:

my @stuff = caller(0);

is the same as:

my @stuff = caller;

which means 0 level up of the call tree so you get what you expect, ie, it 
gives you the information of the function's direct caller.

when you say:

my @stuff = caller(1);

you said give me the parent of the parent (the 1 means one level up of the 
direct caller) and this could return nothing. consider:

#!/usr/bin/perl -w
use strict;

sub fun{
my @stuff = caller(1);
print join("\n",@stuff),"\n" if(@stuff);
}

fun

__END__

prints nothing because there is no 2 level caller tree. you can visualize 
the number as the index to the caller tree. a 1 means the second level of 
the caller. 0 means the first level of the caller tree. in fact, Perl's 
default array index machanism works the same way, 0 means the first 
element, 1 means the second element, etc.

david

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Class using a class: "new" confusion and strange print behavior

2003-03-20 Thread R. Joseph Newton
Rob Dixon wrote:
[snip]

> Yes, but it's
>
> $schedule_day_object->AddTrain($train_name);
>
> which does that for you.
>
> > If that is the case, then your parameter catch would be neater and
> > more understandable as:
> > my ($self, $trainName) = @_;
> >
> > or:
> >   my $self = shift;
> >   my $trainName = shift;
>
> It's a tradition that methods start with:
>
> my $self = shift;
> my ( $trainName, $p1, $p2, $p3, @prest ) = @_;
>
> to separate the implicit parameter from the explcit one.
> But what Rob R wrote will do just as well.

Thanks.  Let me make sure I have this clear, then.  Does any call to a class method 
then have the reference to the object, or the object itself, as the first parameter.  
In that case, it certainly does make sense to shift it back out before handling the 
rest.of the arguments

Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Class using a class: "new" confusion and strange print behavior

2003-03-20 Thread Rob Dixon
R. Joseph Newton wrote:
> Rob Dixon wrote:
>
> > > When I run perl -c on this, I get the following messages:
> > > Subroutine new redefined at Train.pm line 106.
>
> This can be easily corrected:
> my $motto = "Where no one has gone before!";
> ..and still avoid having that pointless damn number at the end of the
> module.

You're right. I'm surprised you don't get a 'Name used only once' warning
but it seems to work. Perhaps the warning applies only to globals.

Rob




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: next if........ a cleaner way?

2003-03-20 Thread R. Joseph Newton
chad kellerman wrote:

> Hello everyone,
>
>  I want to clean this bit of code up.  It looks really messy.  I am in a
> mental block.  Any suggestions?
>
>  @userInfo = split /\s+/, $buffer;
>
> #insert home users & quota in the db
> foreach $userInfo ( @userInfo )  {
>
> ( $name, $quota ) = split /\|/, $userInfo;
> # get rig of the header info from repquota

Right here is where you can start.  Once you trim the user name, lowercase it 
lc($name);
that will provide case insensitivity, presuming that was your intent.  This should 
obviate the need to use such an unwieldy regex.  I would recommend storing that list 
of authorized users in a file, reading it into an array, and then looping through that 
array, using the eq operator.

Beyond aesthetics, ugly and clunky structures have a very practical ill effect:  they 
make code harder to track, and thus increase the likelihood of error.

>
>
> next if ( ( $name =~ /Block/ ) or ( $name =~ /nobody/ ) or ( $name =~
> /User/ ) or ( $name =~ /^\d/ ) or ( $name =~ /www/ )  or ( $name =~ /backup/
> )  or ( $name =~ /ftp/ )  or ( $name =~ /httpd/ ) or ( $name =~ /root/ ) or (
> $name =~ /netop/ ) or ( $name =~ /sysop/ ) or ( $name =~ /users/ ) or (
> $quota !~ /\d+/ ) or ( $name =~ /^#/ ) or ( $quota <= 8 ) or ($name =~
> /bill/) );

Rob already gave you the best suggestion for regex alternatives, the '|' operator.

Another thing that will help you keep your code neat is using a code editor.  there 
are good code editors avaiable for both Winodws and 'nix.  If you use Windows, I 
recommend Programmer's File Editor, available free at 
http://www.lancs.ac.uk/people/cpaap/pfe/.

You need to get a better understanding of indentation conventions.  Programming 
languages use indentation to mark execution blocks.  Statements that are executed in 
sequence should be kept aligned.  When a block, whether a subroutine definition, a 
conditional, a loop, or any other structures that sets off a set of lines for separate 
execution sequence, those lines affected should be indented by a specific [your taste, 
but preferably no more than four] number of *spaces* [tabs don;t translate well].

for (1..10) {
  do_one_thing();
  then_another;
  if ($some_condition_exists) {
take_first_response_step();
follow_up_with_response();
  } else {
take_alternate_course();
  }
}

Notice something?  With absolutely no coded specifics, you know how this program is 
going to execute, where the effect of any control statement starts and stops, and what 
the overall flow of the logic will be.  Good, consistent indentation habits will make 
your work much easier.

Let's take another crack at your problem here:


my @names = qw (
  Block nobody User www backup ftp httpd root
  netop sysop users
)

foreach $userInfo ( @userInfo )  {

  my ($name, $quota ) = split /\|/, $userInfo;
  # get rig of the header info from repquota
  my $matched;
  foreach (@names) {
if lc($_) eq lc($name) {
  matched = 1;
  last;
}
  }
  if ($match or ($name =~ /^\d/) or ( $quota !~ /\d+/ ) or
  ( $name =~ /^#/ ) or ( $quota <= 8 )  or ($name =~ /bill/)) {
next;
  }
}

Note that I pulled out the or'ed conditions that were not really logically parralel.  
Having them mixed in that way can be a bit dangerous to comprehension.  Is there any 
reason you had the conditions for $quota interleaved with those for $name in this way?

Joseph



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: Filling out forms on the web.

2003-03-20 Thread Dan Muey

> Randal Schwartz just wrote an excellent article titled 
> "Screen Scraping for Fun and Profit" in Linux Magazine. His 
> code is available at 
> http://www.linuxmagazine.com/downloads/2003-> 04/perl, although 
> I recommend reading the article (April 
> 2003 edition), because he walks the code line by line and 
> gives you an in depth understanding about what is going on.
> 
> The module WWW:Mechanize lets you do things like fill out 
> forms, "click" on links, etc.

Cool a new (to me anyway) module to play with. Cpan here I come!

> 
> The module HTML::Parser might also help you extract the 
> information you 
> need in a relatively painless way.
> 
> Despite the plug, I am not associated with Randal or Linux 
> Magazine in any way, other than being a subscriber. :)
> 
> Pete
> 
> Mar 20, 2003 at 10:38am from Larry Wissink:
> 
> LW> been able to do comes from reading "Perl & LWP" by Sean M. Burke 
> LW> (O'Reilly Press). I include what I have managed to write, drawn 
> LW> heavily from the Burke book, for the edification (or 
> amusement) of 
> LW> others.  Again, any insight will be greatly appreciated.
> 
> -- 
> http://emerson.wss.yale.edu/perl
> Pete Emerson
> WSS AM&T Yale University
> 
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Filling out forms on the web.

2003-03-20 Thread Pete Emerson
Randal Schwartz just wrote an excellent article titled "Screen Scraping
for Fun and Profit" in Linux Magazine. His code is available at
http://www.linuxmagazine.com/downloads/2003-04/perl, although I recommend
reading the article (April 2003 edition), because he walks the code line
by line and gives you an in depth understanding about what is going on.

The module WWW:Mechanize lets you do things like fill out forms,
"click" on links, etc.

The module HTML::Parser might also help you extract the information you 
need in a relatively painless way.

Despite the plug, I am not associated with Randal or Linux Magazine in any
way, other than being a subscriber. :)

Pete

Mar 20, 2003 at 10:38am from Larry Wissink:

LW> been able to do comes from reading "Perl & LWP" by Sean M. Burke (O'Reilly
LW> Press).
LW> I include what I have managed to write, drawn heavily from the Burke book,
LW> for the edification (or amusement) of others.  Again, any insight will be
LW> greatly appreciated.

-- 
http://emerson.wss.yale.edu/perl
Pete Emerson
WSS AM&T Yale University


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Class using a class: "new" confusion and strange print behavior

2003-03-20 Thread Rob Dixon
Hi Joseph.

R. Joseph Newton wrote:
> Rob Richardson wrote:
>
> > Greetings!
> >
> > I have the following class in a .pm file:
> >
> > use warnings;
> > use strict;
> > package ScheduleDay;
> >
> > use Train;
> >
[snip] 'new' method
> >
> > sub AddTrain
> > {
> > my $self = shift;
> > my $trainName = $_[0];
>
> How are you calling this function.  the formulation above looks like
> the call should be:
> AddTrain($schedule_day_object, $train_name);

Yes, but it's

$schedule_day_object->AddTrain($train_name);

which does that for you.

> If that is the case, then your parameter catch would be neater and
> more understandable as:
> my ($self, $trainName) = @_;
>
> or:
>   my $self = shift;
>   my $trainName = shift;

It's a tradition that methods start with:

my $self = shift;
my ( $trainName, $p1, $p2, $p3, @prest ) = @_;

to separate the implicit parameter from the explcit one.
But what Rob R wrote will do just as well.

> > # $self->{$trainName} = new Train;
> > # $self->{$trainName}->Load(@_);
> > print "   Added train named ", $trainName, "";
> > }
> >
> > "Where no one has gone before!";
> >
> > When I run perl -c on this, I get the following messages:
> > Subroutine new redefined at Train.pm line 106.
> > Useless use of a constant in void context at scheduleday.pm line 24.
> > scheduleday.pm syntax OK
>
> Check the package statement in Train.pm, and try compiling it
> separately before using it in this file.
>
> > If I try to print the name of the train as contained in $printName
> > (and as
> > shown above), the result age is never displayed and IE just waits
> > and
> > waits and waits for something that never happens.
>
> $trainName ain't there.  You've already commented out to lines
> dependent on this variable.  Presumably this was because they raised
> errors.  Check how you are getting a value into $trainName.

It looks good to me, but I'm still waiting on the code for Train.pm where
all the bugs really are :)

Rob




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



caller() problems

2003-03-20 Thread Hanson, Rob
I'm using caller() to get the name of the calling package and method, but
sometimes the info isn't there.  My code looks something like this...

# Begin code example
##
package main;

use ModuleX 'logit';

logit("a message");

package ModuleX;

sub logit
{
  my @caller = caller(1);
  ...do some stuff...
}
##
# End code example

In cases where it doesn't work (it almost always works) it returns 'ModuleX'
as the package instead of 'main'.

I am guessing the problem is (as stated in the perldoc) that "the optimizer
might have optimized call frames away before "caller" had a chance to get
the information.".  Furthermore it states that ""caller(N)" might not return
information about the call frame you expect it do, for "N > 1"".  ...But in
my case "N" never exceeds 1.

Any ideas?

Thanks.

Rob
 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Class using a class: "new" confusion and strange print behavior

2003-03-20 Thread Rob Richardson
Joseph,

That's an interesting thought!  I'll check tonight.  

Rob

--- "R. Joseph Newton" <[EMAIL PROTECTED]> wrote:
> Just a hunch, since the material in question is out of reach, but
> could this be a circular reference?  It happens a lot when assembling
> C headers.  If Train.pm by any chance has a "use ScheduleDay;"
> statement, then that would porobably cause some havoc.



__
Do you Yahoo!?
Yahoo! Platinum - Watch CBS' NCAA March Madness, live on your desktop!
http://platinum.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Class using a class: "new" confusion and strange print behavior

2003-03-20 Thread Rob Richardson
Joseph,

Thanks for your comments.  Let's see if I can snip this and still come
up with intelligible contexts.

--- "R. Joseph Newton" <[EMAIL PROTECTED]> wrote:
> Rob Richardson wrote:
> 
> > Greetings!
> >
> > I have the following class in a .pm file:
> >
> > use warnings;
> > use strict;
> > package ScheduleDay;
> >
> > use Train;
> >
> > sub new
> > {
> > my $self = {};  # $self is a reference to an anonymous,
> empty (for
> > now) hash
> > bless $self;
> > return $self;
> > }
> >
> > sub AddTrain
> > {
> > my $self = shift;
> > my $trainName = $_[0];
> 
> How are you calling this function.  the formulation above looks like
> the call should be:
> AddTrain($schedule_day_object, $train_name);

I have created a ScheduleDay object within my Schedule class by calling
ScheduleDay->new().  Because the return value from new() is blessed, I
don't need to pass in the ScheduleDay object explicitly.  Or at least,
that's how I understand it.

The parameter I explictly pass in to AddTrain is an array of 14
elements, the first of which is the train name.  I am using the name
here as the key to a hash element, and I am also going to store it in
the Train object through the call to Train->Load(), so I don't want to
shift it out of the array.



> > # $self->{$trainName} = new Train;
> > # $self->{$trainName}->Load(@_);
> > print "   Added train named ", $trainName, "";
> > }
> >
> > "Where no one has gone before!";
> 
> $trainName ain't there.  You've already commented out to lines
> dependent on this variable.  Presumably this was because they raised
> errors.  Check how you are getting a value into $trainName. 
> Something is going wrong there.
> 
> Joseph

Your presumption is not correct.  I commented them out because I didn't
want to test the new() and Load() methods yet.  I just wanted to make
sure that the AddTrain() method was getting called as expected and was
finding the train name as expected.  Once I got that point, I was going
to remove the #s and see if those functions worked.  Unfortunately, I
haven't got to that point yet.

Rob

__
Do you Yahoo!?
Yahoo! Platinum - Watch CBS' NCAA March Madness, live on your desktop!
http://platinum.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Filling out forms on the web.

2003-03-20 Thread Larry Wissink
Hi,
I'm a Perl newbie in way over my head, but I love a challenge.  
My company needs to download transaction data from a partner's website on a
regular basis.  The data can be retrieved through a form, (entering date
ranges, selecting merchants, etc.), but one must log in to the site first
and must use Netscape 4.0 or IE 5.0 or higher.  I am trying to use LWP and
related Perl modules to emulate a Netscape browser, login to the site and
then fill out the form.
Unfortunately, I just don't know enough about emulating a browser or secure
transactions to do the job.  I am hoping someone out there can point me to
some resources for learning more about these topics.  Most of what I have
been able to do comes from reading "Perl & LWP" by Sean M. Burke (O'Reilly
Press).
I include what I have managed to write, drawn heavily from the Burke book,
for the edification (or amusement) of others.  Again, any insight will be
greatly appreciated.

Sincerely,

Larry Wissink
[EMAIL PROTECTED]

This script retrieves the login page.  I pipe this to a file from the
command line:
c:\do_get.pl > befree_login.html

#perl
use strict;
use LWP;
use HTTP::Cookies;
my $browser;


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Class using a class: "new" confusion and strange print behavior

2003-03-20 Thread R. Joseph Newton
[EMAIL PROTECTED] wrote:

>
> I was hoping someone would correct me, if you have? ;-)  After reading Bob's e-mail 
> I paid closer attention to the error and thought in terms of compile time rather 
> than runtime... But I am a bit confused about the order of compiling, does the whole 
> file get compiled, then the module, and then they get "linked" or is it sequential 
> so that as soon as a non-previously compiled 'use' is reached, then the that is 
> compiled and then returns to the original point to finish that file? This latter 
> seems to make more sense, but then that doesn't mean it is the case. I really ought 
> to read up more on Perl internals, but finding the time when I want to make my head 
> hurt is difficult at best ;-)..
>
> http://danconia.org

Just a hunch, since the material in question is out of reach, but could this be a 
circular reference?  It happens a lot when assembling C headers.  If Train.pm by any 
chance has a "use ScheduleDay;" statement, then that would porobably cause some havoc.

It might be worth the bandwidth for Rob R. to post all three files--the two modules as 
well as the driver used to test them.

Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Class using a class: "new" confusion and strange print behavior

2003-03-20 Thread R. Joseph Newton
Rob Richardson wrote:

> Greetings!
>
> I have the following class in a .pm file:
>
> use warnings;
> use strict;
> package ScheduleDay;
>
> use Train;
>
> sub new
> {
> my $self = {};  # $self is a reference to an anonymous, empty (for
> now) hash
> bless $self;
> return $self;
> }
>
> sub AddTrain
> {
> my $self = shift;
> my $trainName = $_[0];

How are you calling this function.  the formulation above looks like the call should 
be:
AddTrain($schedule_day_object, $train_name);

If that is the case, then your parameter catch would be neater and more understandable 
as:
my ($self, $trainName) = @_;

or:
  my $self = shift;
  my $trainName = shift;


> # $self->{$trainName} = new Train;
> # $self->{$trainName}->Load(@_);
> print "   Added train named ", $trainName, "";
> }
>
> "Where no one has gone before!";
>
> When I run perl -c on this, I get the following messages:
> Subroutine new redefined at Train.pm line 106.
> Useless use of a constant in void context at scheduleday.pm line 24.
> scheduleday.pm syntax OK

Check the package statement in Train.pm, and try compiling it separately before using 
it in this file.

> What is going on here?  Why can't I have a class that uses another
> class and have both classes have a "new" method?  What do I have to do
> to get this scoped correctly?
>
> Also, I am getting strange behavior when running this through Internet
> Explorer.  If the print statement in AddTrain() is modified to avoid
> the use of $trainName, the program runs as expected and IE displays a
> page with one line of print for every train I try to add.  However, if
> I try to print the name of the train as contained in $printName (and as
> shown above), the result age is never displayed and IE just waits and
> waits and waits for something that never happens.

$trainName ain't there.  You've already commented out to lines dependent on this 
variable.  Presumably this was because they raised errors.  Check how you are getting 
a value into $trainName.  Something is going wrong there.

Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Class using a class: "new" confusion and strange print behavior

2003-03-20 Thread R. Joseph Newton
Rob Dixon wrote:

> > When I run perl -c on this, I get the following messages:
> > Subroutine new redefined at Train.pm line 106.

This can be easily corrected:
my $motto = "Where no one has gone before!";
..and still avoid having that pointless damn number at the end of the module.

Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



%HASHES/Associative Arrays To receive input

2003-03-20 Thread Horace Franklin Jr.
Clear DayHello,

I would like to use %HASHES/Associative arrays to accept input from a 
form generated within a perl script.

The normal input of a html form is print " \n";
Is the syntax when using Hashes:
my %hashes;
print ""\n;

Also what is the syntax is you want to input checkboxes with the same name and 
different name?

Horace

Re: next if........ a cleaner way?

2003-03-20 Thread Rob Dixon
Hi Chad.

Chad Kellerman wrote:
> Hello everyone,
>
>  I want to clean this bit of code up.  It looks really messy.  I
> am in a mental block.  Any suggestions?
>
>  @userInfo = split /\s+/, $buffer;

Use a single space here to split on.

@userInfo = split ' ', $buffer;

It's a special case that splits on contiguous whitespace
like your expression, but discards a null first field if
there are leading spaces in the object string.

> #insert home users & quota in the db
> foreach $userInfo ( @userInfo )  {
>
> ( $name, $quota ) = split /\|/, $userInfo;
> # get rig of the header info from repquota

I hope you have used 'strict' and 'warnings'? I
haven't seen any declarations!

> next if ( ( $name =~ /Block/ ) or ( $name =~ /nobody/ ) or (
> $name =~ /User/ ) or ( $name =~ /^\d/ ) or ( $name =~ /www/ )  or (
> $name =~ /backup/ )  or ( $name =~ /ftp/ )  or ( $name =~ /httpd/ )
> or ( $name =~ /root/ ) or ( $name =~ /netop/ ) or ( $name =~ /sysop/
> ) or ( $name =~ /users/ ) or ( $quota !~ /\d+/ ) or ( $name =~ /^#/ )
> or ( $quota <= 8 ) or ($name =~ /bill/) );
>
> Is there an easier way to loop thru a bunch of regex?

You could do:

next if $name =~ /Block/;
next if $name =~ /nobody/;
:
next if $name =~ /bill/;

which looks a lot better (and shouldn't be any slower).

Note that you're finding these strings anywhere in
the object string. Is that what you want? Also, have
you considered whether you need a case-sensitive
match?

The checks on $quota aren't quite right. The test

next if $quota !~ /\d+/

will pass the string 'B52s' no problem, but the test

next if $quota <= 8

will give you a warning unless you have:

no warnings 'numeric';

and will evaluate the object string as zero anyway. You
might like:

next unless $quota =~ /(\d+)/ and $1 > 8;

which will check that the string contains a number, and
if so that it's more than eight, but I don't know what
your data looks like.

This code should do the trick.

foreach my $userInfo ( split ' ', $buffer )  {

my ( $name, $quota ) = split /|/, $userInfo;

next if grep $name =~ /$_/, qw(
^# ^\d backup bill Block ftp httpd netop
nobody root sysop User users www );

next unless $quota =~ /(\d+)/ and $1 > 8;
}

but remember the provisos above.

I can't help thinking though, that there's probably
a better set of criteria to 'get rid of the header
info'. Let us see your data format if yu need any
further help.

HTH,

Rob




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: why objects?

2003-03-20 Thread Paul Johnson
On Thu, Mar 06, 2003 at 10:25:57AM -0800, Paul wrote:

> I'm sold -- I write most code as object modules now.
> 
>   My coworkers aren't interested in dealing
> with the learning curve for objects, and don't care to hear about it.

I think you have bigger problems than teaching your coworkers about
objects.  Sounds like they wouldn't take well to using a functional
style either, or learning about logic programming.  What about test
driven development, or XP?

Whilst different paradigms and methodologies suit different problems,
you can only use them if you know about them.  I cannot understand a
professional in any field, and particularly one as young as ours, who
wouldn't want to keep learning and improving.  Doubly so if they can do
it on company time.

> Obviously, though I understand it well enough to use it, I can't
> explain the advantages. They say "you could just do that with
> so-and-so", and I can't communicate the amount of work saved by the
> abstraction.

Since (virtually) all computer languages are turing complete, maybe you
should suggest all future development be done using vi macros.

Presumably you are using Perl for the productivity benefits it brings.
Used appropriately, OO can provide further benefits.  You're also
cutting yourself off from a large part of CPAN if you don't want to deal
with objects.

Having said that, OO has been heavily hyped, and there is bound to be a
backlash against that.  It is not a panacea, but it is very effective in
its place.

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Multi-OS script

2003-03-20 Thread beau
Hi folks,

I am trying to write a simple 'status' script
that will run as a simple server on each machine
on a diverse network (there are >400 machines
of all types, OSs and flavors); when queried
the script will resopond (proving that machine
is up) with machine name, ipaddr, disk usage,
and so on.

My problem is that I would like to have only
one script (to simplify maintanence and deployment)
for all OSs. So, for example, for Win32 machines
I may need Win32::API and Win32::DriveInfo; if I
use this contstruct:

  ...
  if ($^O eq 'MSWin32') {
use Win32::API;
use Win32::DriveInfo;
...
  } ...

the script doesn't compile under non-Win32 systems.
I can encapsulate the uses in a string eval, but
then it seems I must put all use of that (thoese)
modules into the same string eval, which gets
syntatically sticky quickly.

Can someone point me to a simple, elegant, perlish
solution?

Aloha => Beau;


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: Unix ls -lrt | tail -1 in Perl

2003-03-20 Thread Hanson, Rob
If you use the first version that Sudarshan posted, you might want to use
this instead.  It uses map, and is a lot faster.

my $latest = (sort {$b->{mtime} <=> $a->{mtime}}
 map {{mtime => -M $_, file => $_}}
 <$dir/*>)[-1];

print $latest->{file}, "\n";

Benchmark results with and without using map:

Benchmark: timing 100 iterations of No-MAP, With-MAP...
No-MAP: 13 wallclock secs ( 2.43 usr  6.43 sys +  4.00 cusr  1.41 csys =
0.00 CPU)
  With-MAP:  8 wallclock secs ( 1.57 usr  0.81 sys +  4.04 cusr  1.27 csys =
0.00 CPU)

Using map allows -M to be performed on each file only once (instead of once
per sort iteration).  And since this is an expensive operation doing it only
once will save a lot of CPU time.

Rob


-Original Message-
From: Sudarshan Raghavan [mailto:[EMAIL PROTECTED]
Sent: Thursday, March 20, 2003 9:17 AM
To: Perl beginners
Subject: Re: Unix ls -lrt | tail -1 in Perl


On Thu, 20 Mar 2003, NYIMI Jose (BMB) wrote:

> Hello,
> 
> How can I do this unix command  in Perl ?  : ls -lrt | tail -1
> Actually, i would like to get the most recent file from a given directory.

my $latest = (sort {-M $b <=> -M $a} <$dir/*>)[-1];

or

my $latest;
while (<$dir/*>) {
$latest = (defined ($latest) && (-M $_ > -M $latest)) ? $latest : $_;
}

Remember to check for the definedness of $latest if $dir is an empty 
directory

> 
> Thanks in advance for your input.
> 
> José.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: next if........ a cleaner way?

2003-03-20 Thread Hanson, Rob
This should work...

next if $name =~
/Block|nobody|User|www|backup|ftp|httpd|root|netop|sysop|users|bill/;
next if $name =~ /^(?:\d|#)/;
next if $quota !~ /\d+/;
next if $quota <= 8;

You should also be able to combine the two (just make sure you test my
syntax)...

next if $name =~
/(?:^(?:\d|#))|Block|nobody|User|www|backup|ftp|httpd|root|netop|sysop|users
|bill/;

And the last two can be combined as well...

next if $quota <= 8;

The reason this works is because if $quota does not start with a number it
will evaluate to 0 in this context.

Rob


-Original Message-
From: chad kellerman [mailto:[EMAIL PROTECTED]
Sent: Thursday, March 20, 2003 9:34 AM
To: [EMAIL PROTECTED]
Subject: next if a cleaner way?


Hello everyone,

 I want to clean this bit of code up.  It looks really messy.  I am in a

mental block.  Any suggestions?


 @userInfo = split /\s+/, $buffer;

#insert home users & quota in the db
foreach $userInfo ( @userInfo )  {

( $name, $quota ) = split /\|/, $userInfo;
# get rig of the header info from repquota
   
next if ( ( $name =~ /Block/ ) or ( $name =~ /nobody/ ) or ( $name
=~ 
/User/ ) or ( $name =~ /^\d/ ) or ( $name =~ /www/ )  or ( $name =~ /backup/

)  or ( $name =~ /ftp/ )  or ( $name =~ /httpd/ ) or ( $name =~ /root/ ) or
( 
$name =~ /netop/ ) or ( $name =~ /sysop/ ) or ( $name =~ /users/ ) or ( 
$quota !~ /\d+/ ) or ( $name =~ /^#/ ) or ( $quota <= 8 ) or ($name =~ 
/bill/) );


Is there an easier way to loop thru a bunch of regex?

Thanks for the help.

Chad




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



next if........ a cleaner way?

2003-03-20 Thread chad kellerman
Hello everyone,

 I want to clean this bit of code up.  It looks really messy.  I am in a 
mental block.  Any suggestions?


 @userInfo = split /\s+/, $buffer;

#insert home users & quota in the db
foreach $userInfo ( @userInfo )  {

( $name, $quota ) = split /\|/, $userInfo;
# get rig of the header info from repquota
   
next if ( ( $name =~ /Block/ ) or ( $name =~ /nobody/ ) or ( $name =~ 
/User/ ) or ( $name =~ /^\d/ ) or ( $name =~ /www/ )  or ( $name =~ /backup/ 
)  or ( $name =~ /ftp/ )  or ( $name =~ /httpd/ ) or ( $name =~ /root/ ) or ( 
$name =~ /netop/ ) or ( $name =~ /sysop/ ) or ( $name =~ /users/ ) or ( 
$quota !~ /\d+/ ) or ( $name =~ /^#/ ) or ( $quota <= 8 ) or ($name =~ 
/bill/) );


Is there an easier way to loop thru a bunch of regex?

Thanks for the help.

Chad




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Unix ls -lrt | tail -1 in Perl

2003-03-20 Thread Sudarshan Raghavan
On Thu, 20 Mar 2003, NYIMI Jose (BMB) wrote:

> Hello,
> 
> How can I do this unix command  in Perl ?  : ls -lrt | tail -1
> Actually, i would like to get the most recent file from a given directory.

my $latest = (sort {-M $b <=> -M $a} <$dir/*>)[-1];

or

my $latest;
while (<$dir/*>) {
$latest = (defined ($latest) && (-M $_ > -M $latest)) ? $latest : $_;
}

Remember to check for the definedness of $latest if $dir is an empty 
directory

> 
> Thanks in advance for your input.
> 
> José.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Class using a class: "new" confusion and strange print behavior

2003-03-20 Thread wiggins


On Thu, 20 Mar 2003 14:06:56 -, "Rob Dixon" <[EMAIL PROTECTED]> wrote:

> Rob Dixon wrote:
> > > When I run perl -c on this, I get the following messages:
> > > Subroutine new redefined at Train.pm line 106.
> >
> > It's telling you that you have two definitions of ScheduleDay::new.
> > Presumably Train.pm has no 'package Train;' statement?
> 
> Wiggins' post got me to think a little more carefully, and it is clear
> that Train.pm is doing the redefining at its line 106, not ScheduleDay.
> That means that the definition must already exist when the 'use Train'
> statement in ScheduleDay is executed, as none of the Train code
> is executed anywhere else. Since 'use' is an implied BEGIN block I
> can't see any way this is possible.
> 
> Can you put me out of my misery Rob, and tell us what's on Train.pm
> line 106?
> 

I was hoping someone would correct me, if you have? ;-)  After reading Bob's e-mail I 
paid closer attention to the error and thought in terms of compile time rather than 
runtime... But I am a bit confused about the order of compiling, does the whole file 
get compiled, then the module, and then they get "linked" or is it sequential so that 
as soon as a non-previously compiled 'use' is reached, then the that is compiled and 
then returns to the original point to finish that file? This latter seems to make more 
sense, but then that doesn't mean it is the case. I really ought to read up more on 
Perl internals, but finding the time when I want to make my head hurt is difficult at 
best ;-)..

http://danconia.org

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Class using a class: "new" confusion and strange print behavior

2003-03-20 Thread Rob Dixon
Rob Dixon wrote:
> > When I run perl -c on this, I get the following messages:
> > Subroutine new redefined at Train.pm line 106.
>
> It's telling you that you have two definitions of ScheduleDay::new.
> Presumably Train.pm has no 'package Train;' statement?

Wiggins' post got me to think a little more carefully, and it is clear
that Train.pm is doing the redefining at its line 106, not ScheduleDay.
That means that the definition must already exist when the 'use Train'
statement in ScheduleDay is executed, as none of the Train code
is executed anywhere else. Since 'use' is an implied BEGIN block I
can't see any way this is possible.

Can you put me out of my misery Rob, and tell us what's on Train.pm
line 106?

Rob




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Unix ls -lrt | tail -1 in Perl

2003-03-20 Thread NYIMI Jose (BMB)
Hello,

How can I do this unix command  in Perl ?  : ls -lrt | tail -1
Actually, i would like to get the most recent file from a given directory.

Thanks in advance for your input.

José.


 DISCLAIMER 

"This e-mail and any attachment thereto may contain information which is confidential 
and/or protected by intellectual property rights and are intended for the sole use of 
the recipient(s) named above. 
Any use of the information contained herein (including, but not limited to, total or 
partial reproduction, communication or distribution in any form) by other persons than 
the designated recipient(s) is prohibited. 
If you have received this e-mail in error, please notify the sender either by 
telephone or by e-mail and delete the material from any computer".

Thank you for your cooperation.

For further information about Proximus mobile phone services please see our website at 
http://www.proximus.be or refer to any Proximus agent.



RE: Class using a class: "new" confusion and strange print behavi or

2003-03-20 Thread Rob Richardson
Greetings!

Thanks to all of you for your thoughts.  I like Rob's idea of ORing
$_[0] with 'The Little Engine That Could'.  I'll try that.  I'm pretty
darn sure that Train.pm has a line that says "package Train;", but I'll
double-check.  

A bit of background:  This program will eventually be used to manage
the process of volunteers signing up over the Internet to work on
trains run by the Cuyahoga Valley Scenic Railroad.  I have a Schedule
class to manage the overall process, a ScheduleDay class (whose file
name is "ScheduleDay.pm" to manage the group of trains that will run on
a particular day, and a Train class to manage an individual train.  I
am using the Apache server on my local machine, and running my test
program through Internet Exporer, as in
http://localhost/crew/traintest.cgi .  

Thanks again!

RobR


--- Bob Showalter <[EMAIL PROTECTED]> wrote:
> Rob Richardson wrote:
> > Greetings!
> > 
> > I have the following class in a .pm file:
> > 
> > use warnings;
> > use strict;
> > package ScheduleDay;
> > 
> > use Train;
> > 
> > sub new
> > {
> > my $self = {};  # $self is a reference to an anonymous, empty (for
> > now) hash
> > bless $self;
> > return $self;
> > }
> 
> n.b. this constructor is not inheritable if you ever derive a
> subclass from
> ScheduleDay. You might want to consider using:
> 
>sub new {
>   my $class = shift;
>   $class = ref $class || $class;
>   bless {}, $class;
>}
> 
> > 
> > sub AddTrain
> > {
> > my $self = shift;
> > my $trainName = $_[0];
> > # $self->{$trainName} = new Train;
> > # $self->{$trainName}->Load(@_);
> > print "   Added train named ", $trainName, ""; }
> > 
> > "Where no one has gone before!";
> > 
> > 
> > When I run perl -c on this, I get the following messages:
> > Subroutine new redefined at Train.pm line 106.
> 
> This is likely a problem in Train.pm, which you didn't share with us.
> Do you
> get the same message from perl -c Train.pm?
> 
> > Useless use of a constant in void context at scheduleday.pm line
> 24.
> 
> A warning for the phrase. If you don't like the warning, replace the
> phrase
> with the more idiomatic literal 1;
> 
> > scheduleday.pm syntax OK 
> 
> So scheduleday.pm compiles OK. But it should be named ScheduleDay.pm.
> 
> > 
> > What is going on here?  Why can't I have a class that uses another
> > class and have both classes have a "new" method?  What do I have to
> > do to get this scoped correctly? 
> 
> You can have this. Your problems lie elsewhere.
> 
> > 
> > Also, I am getting strange behavior when running this through
> Internet
> > Explorer.
> 
> Running what through Internet Explorer? Assuming code not in evidence
> :~)
> 
> > If the print statement in AddTrain() is modified to avoid
> > the use of $trainName, the program runs as expected and IE displays
> a
> > page with one line of print for every train I try to add.  However,
> if
> > I try to print the name of the train as contained in $printName
> (and
> > as shown above), the result age is never displayed and IE just
> waits
> > and waits and waits for something that never happens.
> 
> ???
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 


__
Do you Yahoo!?
Yahoo! Platinum - Watch CBS' NCAA March Madness, live on your desktop!
http://platinum.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: Class using a class: "new" confusion and strange print behavi or

2003-03-20 Thread Bob Showalter
Rob Richardson wrote:
> Greetings!
> 
> I have the following class in a .pm file:
> 
> use warnings;
> use strict;
> package ScheduleDay;
> 
> use Train;
> 
> sub new
> {
>   my $self = {};  # $self is a reference to an anonymous, empty (for
> now) hash
>   bless $self;
>   return $self;
> }

n.b. this constructor is not inheritable if you ever derive a subclass from
ScheduleDay. You might want to consider using:

   sub new {
  my $class = shift;
  $class = ref $class || $class;
  bless {}, $class;
   }

> 
> sub AddTrain
> {
>   my $self = shift;
>   my $trainName = $_[0];
>   # $self->{$trainName} = new Train;
>   # $self->{$trainName}->Load(@_);
>   print "   Added train named ", $trainName, ""; }
> 
> "Where no one has gone before!";
> 
> 
> When I run perl -c on this, I get the following messages:
> Subroutine new redefined at Train.pm line 106.

This is likely a problem in Train.pm, which you didn't share with us. Do you
get the same message from perl -c Train.pm?

> Useless use of a constant in void context at scheduleday.pm line 24.

A warning for the phrase. If you don't like the warning, replace the phrase
with the more idiomatic literal 1;

> scheduleday.pm syntax OK 

So scheduleday.pm compiles OK. But it should be named ScheduleDay.pm.

> 
> What is going on here?  Why can't I have a class that uses another
> class and have both classes have a "new" method?  What do I have to
> do to get this scoped correctly? 

You can have this. Your problems lie elsewhere.

> 
> Also, I am getting strange behavior when running this through Internet
> Explorer.

Running what through Internet Explorer? Assuming code not in evidence :~)

> If the print statement in AddTrain() is modified to avoid
> the use of $trainName, the program runs as expected and IE displays a
> page with one line of print for every train I try to add.  However, if
> I try to print the name of the train as contained in $printName (and
> as shown above), the result age is never displayed and IE just waits
> and waits and waits for something that never happens.

???

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: Class using a class: "new" confusion and strange print behavior

2003-03-20 Thread wiggins


On Wed, 19 Mar 2003 20:02:20 -0800 (PST), Rob Richardson <[EMAIL PROTECTED]> wrote:

> Greetings!
> 
> I have the following class in a .pm file:
> 
> use warnings;
> use strict;
> package ScheduleDay;
> 
> use Train;
> 
> sub new
> {
>   my $self = {};  # $self is a reference to an anonymous, empty (for
> now) hash
>   bless $self;
>   return $self;
> }
> 

Rob's suggestion that you lack a package statement for Train could be it, it could 
also be that Train exports 'new', which is placing it in the current namespace, then 
you are redefining it. Probably you shouldn't export 'new' if it is.

http://danconia.org

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: How can I check the size of a file

2003-03-20 Thread Rob Dixon
Hello Camillo

It seems you're not too well versed with Perl?

Giarrocco, Camillo wrote:
> Rob,
>
> Thank You for your replay.
>
> Let's say I want check if a file is not less then 1 bytes
> I would do then:
>
> $size = "1";

This will work, but "1" is a string, whereas you really mean
a number. The two are interchangeable in Perl. Might as well say

$size = 1;

if that's what you mean.

> If (-s "file" <= $size) {do something...}

This is 'doing something' if the file size is less than or equal
to $size. If you want to do it only "if a file is not less then
1 bytes" you want:

If (-s "file" >= $size) {do something...}

but you may as well hard-code the value here unless you
have a reason not to.

If (-s "file" >= 1) {do something...}

> else{next;}

Finally, if the else is a 'next' you can reduce this to:

next If -s "file" < 1;
do something...;

How does that fit?

Rob

And by the way, please post to the perl.beginners group,
rather than directly to me. Thanks.
/R




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: How can I check the size of a file

2003-03-20 Thread Rob Dixon
Giarrocco, Camillo wrote:
> Hi,
>
> Can anybody tell me how can I check the size of a file?
>
> I know the "if  (-s "file") function but never used and don't know the
> right syntax of it.

Well it's just that really. But you can do more than just test it for
being non-zero with 'if'.

my $size = -s 'file';

and use it as you will.

Rob





-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Class using a class: "new" confusion and strange print behavior

2003-03-20 Thread Rob Dixon
This is a repost, to correctly start a new thread.
Apologies to all for the duplication.

/R

Rob Richardson wrote:
> Greetings!

Hi Rob.

> I have the following class in a .pm file:
>
> use warnings;
> use strict;
> package ScheduleDay;
>
> use Train;
>
> sub new
> {
> my $self = {};  # $self is a reference to an anonymous, empty (for now) hash
> bless $self;
> return $self;

No need for the return statement. The 'bless' function returns the correct
value for a constructor precisely so that it can be used as the last
executable line.

> }
>
> sub AddTrain
> {
> my $self = shift;
> my $trainName = $_[0];

It would be nice to check that a non-empty parameter has been
passed:

my $trainName = $_[0] || 'The Little Engine That Could';

> # $self->{$trainName} = new Train;
> # $self->{$trainName}->Load(@_);
> print "   Added train named ", $trainName, "";
> }
>
> "Where no one has gone before!";
>
>
> When I run perl -c on this, I get the following messages:
> Subroutine new redefined at Train.pm line 106.

It's telling you that you have two definitions of ScheduleDay::new.
Presumably Train.pm has no 'package Train;' statement?

> Useless use of a constant in void context at scheduleday.pm line 24.
> scheduleday.pm syntax OK

As long as you have

use warnings 'void';

in effect, Perl will issue a warning if you use a constant other than the
'standard' true/false values (0, '0', '', undef for false and 1, '1' for true).
Replace

"Where no one has gone before!";

with

1;

and you will be OK.

> What is going on here?  Why can't I have a class that uses another
> class and have both classes have a "new" method?  What do I have to do
> to get this scoped correctly?
>
> Also, I am getting strange behavior when running this through Internet
> Explorer.  If the print statement in AddTrain() is modified to avoid
> the use of $trainName, the program runs as expected and IE displays a
> page with one line of print for every train I try to add.  However, if
> I try to print the name of the train as contained in $printName

$trainName ?

>(and as shown above), the result age is never displayed and IE just waits
> and waits and waits for something that never happens.

Again, your problem is at a higher plain than we mere mortals can
comprehend.

There's nothing wrong here. All I can suggest is that either there's
something in $trainName that upsets something else somewhere
or you're changing more than just adding the print statement.
We really need to see the calling code, but you may get some
mileage in printing the train name in hex so that you can see what's
actually in it.

print "Added train named ";
printf "%02X ", ord $_ foreach split //, $trainName;
print "\n";

but that's a bit of a long shot.

HTH,

Rob








-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Class using a class: "new" confusion and strange print behavior

2003-03-20 Thread Rob Dixon
Rob Richardson wrote:
> Greetings!

Hi Rob.

> I have the following class in a .pm file:
>
> use warnings;
> use strict;
> package ScheduleDay;
>
> use Train;
>
> sub new
> {
> my $self = {};  # $self is a reference to an anonymous, empty (for now) hash
> bless $self;
> return $self;

No need for the return statement. The 'bless' function returns the correct
value for a constructor precisely so that it can be used as the last
executable line.

> }
>
> sub AddTrain
> {
> my $self = shift;
> my $trainName = $_[0];

It would be nice to check that a non-empty parameter has been
passed:

my $trainName = $_[0] || 'The Little Engine That Could';

> # $self->{$trainName} = new Train;
> # $self->{$trainName}->Load(@_);
> print "   Added train named ", $trainName, "";
> }
>
> "Where no one has gone before!";
>
>
> When I run perl -c on this, I get the following messages:
> Subroutine new redefined at Train.pm line 106.

It's telling you that you have two definitions of ScheduleDay::new.
Presumably Train.pm has no 'package Train;' statement?

> Useless use of a constant in void context at scheduleday.pm line 24.
> scheduleday.pm syntax OK

As long as you have

use warnings 'void';

in effect, Perl will issue a warning if you use a constant other than the
'standard' true/false values (0, '0', '', undef for false and 1, '1' for true).
Replace

"Where no one has gone before!";

with

1;

and you will be OK.

> What is going on here?  Why can't I have a class that uses another
> class and have both classes have a "new" method?  What do I have to do
> to get this scoped correctly?
>
> Also, I am getting strange behavior when running this through Internet
> Explorer.  If the print statement in AddTrain() is modified to avoid
> the use of $trainName, the program runs as expected and IE displays a
> page with one line of print for every train I try to add.  However, if
> I try to print the name of the train as contained in $printName

$trainName ?

>(and as shown above), the result age is never displayed and IE just waits
> and waits and waits for something that never happens.

Again, your problem is at a higher plain than we mere mortals can
comprehend.

There's nothing wrong here. All I can suggest is that either there's
something in $trainName that upsets something else somewhere
or you're changing more than just adding the print statement.
We really need to see the calling code, but you may get some
mileage in printing the train name in hex so that you can see what's
actually in it.

print "Added train named ";
printf "%02X ", ord $_ foreach split //, $trainName;
print "\n";

but that's a bit of a long shot.

HTH,

Rob






-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]