Re: perl alias

2011-06-11 Thread Uri Guttman
> "BF" == Brian Fraser  writes:

  BF> On Sat, Jun 11, 2011 at 5:15 PM, Mike McClain  wrote:
  >> 
  >> #   set up an alias for the fastest prime generator
  >> *primes = \&sieve_eratosthenese_lucky;
  >> 
  >> 
  BF> Just use that, but wrap it in a BEGIN block.

that isn't going to help. we don't see the actual code doing the
export. here is a working example from File::Slurp:

@EXPORT_OK = (
@edit_export,
qw(
slurp
prepend_file
),
) ;


*slurp = \&read_file ;

you need to export the aliased name as well as the original one. either
can be in EXPORT or in EXPORT_OK.

uri

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

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




Re: Loading library different per Operating System

2011-06-11 Thread Dr.Ruud

On 2011-06-11 17:20, JP wrote:


How can I load a library depending on the platform I run on?


A use statement is always processed at compile time, no matter if there 
are BEGIN blocks or if statements around it, or where it is in the source.


Check out "use if" (see perldoc if), or do the string-evalling yourself.

--
Ruud

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




Re: Loading library different per Operating System

2011-06-11 Thread Shawn H Corey

On 11-06-11 11:20 AM, JP wrote:

How can I load a library depending on the platform I run on?


Try:

BEGIN {
  use English qw( -no_match_vars );

  if ( $OSNAME eq "linux" ) {
use Device::SerialPort qw( :PARAM :STAT 0.07 );
  }
  elsif ( $OSNAME eq "MSWin32" ) {
use Win32::SerialPort  qw( :PARAM :STAT 0.07 );
  }
  else {
die "Unsupported Operating System: $OSNAME\n";
  };
}



--
Just my 0.0002 million dollars worth,
  Shawn

Confusion is the first step of understanding.

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

The secret to great software:  Fail early & often.

Eliminate software piracy:  use only FLOSS.

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




Win32::SerialPort COM4 doesn't claim to be a serial port

2011-06-11 Thread JP

I have a problem with my serial device on Windows, whereas it works fine on 
Linux.


It works fine on Linux:
use Device::SerialPort qw( :PARAM :STAT 0.07 );
$PortObj = new Device::SerialPort ($PortName, $quiet, $lockfile) || die "Can't 
open $PortName: $!\n$file_exists"; }



I get an error on Windows Vista:
use Win32::SerialPort qw( :PARAM :STAT 0.19 );
$PortObj = new Win32::SerialPort ($PortName, $quiet, $lockfile) || die "Can't 
open $PortName: $!\n$file_exists"; }

D:\tmp>.\dvm2000v0.1.11.pl --device="COM4"
=>  doesn't claim to be a serial port
at D:\tmp\dvm2000v0.1.11.pl line 404
Can't open COM4: No such file or directory


Same error when using "\\.\COM4" as device name.
When I unplug the cable, the error message "doesn't claim to be a serial port" 
is not printed.

About the serial device:
dmesg: SPCP8x5 ttyUSB0: SPCP8x5 converter
USB ID: 04fc:0201 Sunplus Technology Co., Ltd

What is going wrong? What can I check to fix this issue?

For more details, refer to 
http://wirespeed.xs4all.nl/mediawiki/index.php/Velleman_DVM2000_PCLink

Thnx!

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




Loading library different per Operating System

2011-06-11 Thread JP

Hi all,

I need to load a different library, depending on the platform I am running on.

When running on Linux, I need "Device::SerialPort";
while running on Windows I need "Win32::SerialPort".

This is what I came up with:

if ( $OS eq "linux" ) { use Device::SerialPort qw( :PARAM :STAT 0.07 ); }
elsif ( $OS eq "MSWin32" ) { use Win32::SerialPort  qw( :PARAM :STAT 0.07 ); }
else { die "Unsupported Operating System: $OS\n"; };

But the loading of the libraries is done during compile and therefore this will allways 
fail with an "Can't locate /SerialPort.pm in @INC".\

How can I load a library depending on the platform I run on?

Thanks!

JP

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




Re: perl alias

2011-06-11 Thread Brian Fraser
On Sat, Jun 11, 2011 at 5:15 PM, Mike McClain  wrote:

>
> #   set up an alias for the fastest prime generator
> *primes = \&sieve_eratosthenese_lucky;
>
>
Just use that, but wrap it in a BEGIN block.


perl alias

2011-06-11 Thread Mike McClain
OK, it's probably not called an alias but I don't know the proper term.
I've been exploring prime number generation algorithms and after putting
the various routines in primes.pm I thought it would be neat to name the
fastest simply primes as so:

#   set up an alias for the fastest prime generator
*primes = \&sieve_eratosthenese_lucky;

and export primes but it doesn't work:
Undefined subroutine &main::primes called at ./primes.pl line 176

I can put the same statement in a file that imports 
sieve_eratosthenese_lucky but that's not what I really wanted.

Is there a way to setup such an alias in the *.pm that will
be correctly exported or am I faced with having to rename
the fastest routine?

Thanks,
Mike
-- 
Satisfied user of Linux since 1997.
O< ascii ribbon campaign - stop html mail - www.asciiribbon.org

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




Re: Looking for comments on code

2011-06-11 Thread sono-io
Dr. Ruud wrote:

> Because of RaiseError, that 'or die' is unreachable code

That's good to know.  I wasn't aware of that.  I'm reading up on it 
now.  Thanks!


Gurpreet Singh wrote:

> Just use a boolean "read_Primary_Key" (default false) and then true after 
> reading the first line.

Now why didn't I think of that! ;)  Of course - that makes sense.  The 
first time through the loop, $primary_key is undefined. So I added:

$primary_key = $key unless defined ($primary_key);

This allowed me to get rid of the redundant $count, and to shrink the 
number of lines by four, as well.

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




Re: Looking for comments on code

2011-06-11 Thread Gurpreet Singh
Since $count is nowhere used again, why increment it each time just for the 
sake of reading primary key. Just use a boolean "read_Primary_Key" (default 
false) and then true after reading the first line.


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




Re: using Perl, How to search for files in my computer

2011-06-11 Thread Shawn H Corey

On 11-06-11 11:14 AM, eventual wrote:

Hi,
I am using windows7.
Using Perl, how do I search for files in my computer, eg to search in c:\ and 
all its sub-directories.
I only know how to use glob to search from a particular location.
Thanks


You can use File::Find, see `perldoc File::Find`.  It is a standard 
module and is installed along with Perl.  For a list of all the 
standards pragmatics and modules, see `perldoc perlmodlib`.



--
Just my 0.0002 million dollars worth,
  Shawn

Confusion is the first step of understanding.

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

The secret to great software:  Fail early & often.

Eliminate software piracy:  use only FLOSS.

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




Re: Looking for comments on code

2011-06-11 Thread Dr.Ruud

On 2011-06-11 04:25, sono...@fannullone.us wrote:


# Connect to the server to create the db, if it doesn't exist already.
   my $dbh_db = DBI->connect("DBI:mysql:database=;$host",
  $username, $password,
  {'RaiseError' =>  1});



The "RaiseError" attribute can be used to force errors to raise exceptions




 $dbh_db->do($qry_db) or die ("Can't create db!\n");


Because of RaiseError, that 'or die' is unreachable code. Etc.

--
Ruud

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




Re: Looking for comments on code

2011-06-11 Thread sono-io
On Jun 11, 2011, at 2:21 AM, Shlomi Fish wrote:

> Very well, I'll comment on your code.

Thanks, Shlomi.  Much appreciated.

>>my $username = 'root';
>>my $password = 'root';
>>my $host = "127.0.0.1:8889";
>>my $db   = "test_dbb";
> I hope you are not connecting to the database as user root with password root.

This is just a MAMP install for testing.


>>if ($dbh_db) { say "Database '$db' was created successfully." }
> That's the wrong way to test for errors.

What do you recommend instead?


>>  my $table = fileparse($file_path, qr/\.[^.]*/);
> What are you trying to do here?

I'm using File::Basename to grab the file name from the path to use as 
the table name.


>>  while (my $line = <$file_fh>) {
>>  last if $line =~ m/#/;  # can place comments below the ### line
> 1. Is this a comment anywhere or only at the beginning of the string?

I was placing a line of  at the end of the file to allow me to add 
comments after it.  However, I realized that someone might include a # in a 
field name, so I changed that line to be  ### COMMENTS ###  instead, and now 
I'm testing for it like so:

last LINES if $line =~ m/### COMMENTS ###/;


>>  if ($keytype) {
>>  $qry_table .= "`$key` $keytype, ";
>>  }
>>  else {
>>  $qry_table .= "`$key` TEXT, ";
>>  }
> 
> 1. This way you'll have a trailing L.


What do you mean by "trailing L"?


Thanks again,
Marc



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




using Perl, How to search for files in my computer

2011-06-11 Thread eventual
Hi,
I am using windows7.
Using Perl, how do I search for files in my computer, eg to search in c:\ and 
all its sub-directories.
I only know how to use glob to search from a particular location.
Thanks

Re: discover all packages subclassing some other package

2011-06-11 Thread Peter Scott
On Sat, 11 Jun 2011 08:25:15 +0200, marcos rebelo wrote:

> I need to discover all the packages (not files, the symbolic tables)
> that are child of my package, something like:
> 
> my @child = map { $_->isa('My::Class') } ->ALL_PACKAGES<-
> 
> How do I create the ->ALL_PACKAGES<- list?

Class::Inspector->subclasses.

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

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




Re: solution for Regex

2011-06-11 Thread John Delacour

At 08:28 -0700 10/06/2011, Gurpreet Singh wrote:


Correct me if i am wrong -

$read = 0 unless /\s[A-Z]{3}:/;
This might pick up wrong values also - since one of the DBLINKS data 
(the first record) might also get picked up - it should match this 
regex.


Run my script with the data and see.  $read is already false at that 
point.  If the data were differently arranged then maybe, but I was 
not proposing a universal solution - just a suggestion as to a 
different approach.


JD



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




Re: Looking for comments on code

2011-06-11 Thread Shlomi Fish
Hi Marc,

On Saturday 11 June 2011 05:25:18 sono...@fannullone.us wrote:
>   O.K.  I've finished my code to create a MySQL db and/or table, and I'd
> appreciate hearing your comments.  It works well, but it needs to be
> cleaned up before going live.  I've left some testing code in.  Go ahead
> and be brutally honest.  I've learned so much from this list already.
> 

Very well, I'll comment on your code.

> Thanks,
> Marc
> 
> -
> 
> #!/usr/local/bin/perl
> 
> use warnings;
> use strict;
> use CGI::Carp "fatalsToBrowser";
> 
> use feature 'say';
> 
> use DBI();

Better have a space between the "DBI" And the "()".

> use CGI;
> my $q = CGI->new();

I personally distaste calling the CGI instance "$q".

> say $q->header(), $q->start_html( -title => "Create MySQL db and table" );
> 
> my $username = 'root';
> my $password = 'root';
> my $host = "127.0.0.1:8889";
> my $db   = "test_dbb";

I hope you are not connecting to the database as user root with password root.

> # table name is set below
> 
> # Connect to the server to create the db, if it doesn't exist already.
>   my $dbh_db = DBI->connect("DBI:mysql:database=;$host",
>  $username, $password,
>  {'RaiseError' => 1});
> 
> 
> my $qry_db = "CREATE DATABASE IF NOT EXISTS $db ";
> $dbh_db->do($qry_db) or die ("Can't create db!\n");

No need for the temporary variable. Just do:

$dbh_db->do("CREATE DATABASE IF NOT EXISTS $db ");

> 
> if ($dbh_db) { say "Database '$db' was created successfully. />" }

That's the wrong way to test for errors.

> 
> # Disconnect from the database.
>   $dbh_db->disconnect();
> 

You should limit the scope of $dbh_db because it's not used after that:

{
my $dbh_db = DBI->connect(..)

.
.
.

$dbh_db->disconnect()
}

> 
> # Connect to the database to create the table and fields.
>   my $dbh_table = DBI->connect("DBI:mysql:$db;$host",
>   $username, $password,
>   {'RaiseError' => 1});
> 
>   my ($primary_key, @fieldnames);
>   my $file_path = "../htdocs/carts/sql/data/orders.tsv";
>   use File::Basename;

Put all the use's at the top.

>   my $table = fileparse($file_path, qr/\.[^.]*/);

What are you trying to do here?

> 
>   open (my $file_fh, '<', $file_path) || die ("Can't open $file_path for
> reading"); my $count = 0;

1. You should break these lines.

2. Put «my $count = 0;» on a separate line.

> 
>   my $qry_table = "CREATE TABLE IF NOT EXISTS $table ( ";


>   while (my $line = <$file_fh>) {
>   last if $line =~ m/#/;  # can place comments below the ### line

1. Is this a comment anywhere or only at the beginning of the string?

2. Always use last/next/redo/etc. with labels:

http://perl-begin.org/tutorials/bad-elements/#flow-stmts-without-labels

3. You should split your code into paragraphs.

>   chomp $line;
>   next if (! $line);  # skip any blank lines in the file...

The "!" will also match 0. Maybe do:

if ($line eq '')
{
next;
}

>   $line =~ tr|a-zA-Z0-9,_\t \(\)||cd;
>   $count++;
>   my @fields = split (/\t/ , $line);
>   push ( @fieldnames, $fields[0] );
>   if ($count == 1) {
>   $primary_key = $fields[0];
>   }
>   my $key = $fields[0];
>   my $keytype = $fields[1];

1. You're using $fields[0] more than one time here.

2. You should unpack an array using:

my ($key, $keytype) = @fields;

>   if ($keytype) {
>   $qry_table .= "`$key` $keytype, ";
>   }
>   else {
>   $qry_table .= "`$key` TEXT, ";
>   }

1. This way you'll have a trailing L.

2. You can use «$keytype ||= 'TEXT'» instead here and have less duplicate 
code.

>   }
>   $qry_table .= "PRIMARY KEY (`$primary_key`) ) ENGINE=MyISAM DEFAULT
> CHARACTER SET latin1 COLLATE latin1_general_ci;";
> 
>   $dbh_table->do($qry_table) or die ("Can't create table!\n");
>   if ($dbh_table) { say "Table '$table' was created successfully.\n" }
> 
>   close $file_fh;
> 
> say $q->end_html();
> 
> $dbh_table->disconnect();
> 

Regards,

Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
Escape from GNU Autohell - http://www.shlomifish.org/open-
source/anti/autohell/

Every successful open-source project will eventually spawn a sub-project.

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

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




Re: discover all packages subclassing some other package

2011-06-11 Thread marcos rebelo
I found an half solution, since it doesn't get me the ->ALL_PACKAGES<-

mro::get_isarev($classname)

How do I get the ->ALL_PACKAGES<-?


Thanks for all your help
Marcos Rebelo

On Sat, Jun 11, 2011 at 08:25, marcos rebelo  wrote:
>
> I need to discover all the packages (not files, the symbolic tables) that are 
> child of my package, something like:
> my @child = map { $_->isa('My::Class') } ->ALL_PACKAGES<-
>
> How do I create the ->ALL_PACKAGES<- list?
>
> Notice that the focus of these question is the creation of ->ALL_PACKAGES<-
>
> Thanks for all your help
> Marcos Rebelo
> --
> Marcos Rebelo
> http://www.oleber.com/
> Webmaster of http://perl5notebook.oleber.com



--
Marcos Rebelo
http://www.oleber.com/
Webmaster of http://perl5notebook.oleber.com

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