[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Omaha.pm] Mtg last Thr, next mtg




I didn't come up with an agenda for our monthly meeting last Thr, so didn't email out a reminder, but Ron showed up anyway. After many years of Java, he's recently been declared "the Perl guy" for his team at work, so he's brushing up his Perl skills he hasn't used in a while. (And he happens to work across the street from my office. Small town, Omaha. -grin-)

Ron and I walked through turning Perl code into packages and objects, and touched briefly on complex nested data structures, references, and CGI.pm, just for fun.

Next meeting: Thursday, November 18th 2004 @ 7pm

   http://omaha.pm.org/

Below is the code we wrote during the meeting.

If anyone has anything they want to go over at the meetings, I'm all ears. Otherwise, I'm just winging it every month. -grin-

Cheers,

j



=================================================
A tiny nested complex data structures example (de/referencing)
==================================================
---------------
j.pl:
---------------
#!/usr/bin/perl

my %hash = (
   'a' => 'A',
   'b' => 'B',
   'c' => {
      blue => 'bluer',
      grey => 'gray',
      peach => [ 7,8,9,10 ]
   }
);

my $ref = \%hash;

print "$$ref{c}{peach}[2]\n";
print "$ref->{c}->{peach}->[2]\n";
print "$ref->{c}{peach}->[2]\n";
print "$ref->{c}{peach}[2]\n";


=================================
Writing our talker object
=================================

---------------
run.pl:
---------------
#!/usr/bin/perl

use talker;

my $t = talker->new();
$t->hello("world");
$t->dance("really fast");
$t->set_jaysmiddlename('Weston');
print "..... middle name: ",
   $t->get_jaysmiddlename(), "\n";


---------------
talker.pm:
---------------
package talker;

use vars qw( $AUTOLOAD );

sub new {
   my %self = (
      who_is_here => {
         # 'Jay' => 71273891,
         # 'Ron' => 12738917,
         # 'JayS' => 127893917,
      },
      hello_count => 0,
      bye_count   => 0
   );

   return bless \%self;
}

#sub get_hello_count {
#   my ($self) = @_;
#   return $self->{hello_count};
#}
#sub get_bye_count {
#   my ($self) = @_;
#   return $self->{bye_count};
#}


sub hello {
   my ($self, $who) = @_;
   $self->{who_is_here}->{$who} = time;
   $self->{hello_count}++;
   print "Hello, $who!\n";
}

sub bye {
   my ($self, $who) = @_;
   $self->warn($who);
   delete %$self->{who_is_here}->{$who};
   $self->{bye_count}++;
   print "Goodbye, $who!\n";
}

sub warn {
   my ($self, $who) = @_;
   print "WARNING: I've been told goodbye for user '$who'\n";
}

sub print_counts {
   my ($self) = @_;
   print "Hello: ",
      $self->get_hello_count,
      " Bye: ",
      $self->get_bye_count,
      "\n";
}


sub AUTOLOAD {
    my ($self, $arg1) = @_;

    my $name = $AUTOLOAD;
    $name =~ s/.*://;   # strip fully-qualified portion

    if ($name =~ /^get_(.*)/) {
       #print "get_$1? Well, I didn't program that, but OK...!\n";
       return $self->{$1};
    } elsif ($name =~ /^set_(.*)/) {
       #print "set_$1? Well, I didn't program that, but OK...!\n";
       $self->{$1} = $arg1;
       return 1;
    } else {
       print "YOU ENVOKED NON-EXISTANT METHOD $name!\n";
    }
}


1;


=================================================
A tiny Date::Calc example (show method exporting)
=================================================

[Jay-Hannahs-Computer:~/tmp3/tmp] jhannah% more run.pl
#!/usr/bin/perl

use Date::Calc qw( Today Add_Delta_Days );

my @date = Today();
my $cnt = 0;
while ($cnt < 10) {
   printf("%04d-%02d-%02d\n", @date);
   @date = Add_Delta_Days(@date, 1);
   $cnt++;
}



=================================================
A tiny CGI demo....
=================================================

[omaha@www omaha]$ more j2.pl
#!/usr/bin/perl

use CGI;

my $q = CGI->new();
my $blah = $q->param('blah');


print
   $q->header,
   $q->start_html,
   $q->h1("Ron's First CGI.pm CGI!"),
   "<h1>Jay likes manual HTML better</h1>",
   "Previous entry: $blah<p>\n",
   $q->start_form;

if ($q->param('blah3')) {
   print "Yes sir! I will process blah3 immediately!<br>\n";
}

for (1..10) {
   print $q->textfield("blah$_"), "<br>\n";
}
print
   $q->submit,
   $q->end_form,
   $q->end_html;