Re: help on perlform

2010-10-07 Thread Shlomi Fish
On Wednesday 06 October 2010 17:44:20 Tony Frasketi wrote:
 H K.V..

Who, or what is K.V.?

 Might want to try again... I googled and got About 15,600 results (0.23
 seconds) 
 
try. perldoc.perl.org/*perlform*.html

Like I said http://search.cpan.org/dist/Perl6-Form/ would be a better idea 
than perlform, in the rare case that you'll need such functionality.

Regards,

Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
http://www.shlomifish.org/humour/ways_to_do_it.html

rindolf She's a hot chick. But she smokes.
go|dfish She can smoke as long as she's smokin'.

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: help on perlform

2010-10-07 Thread Shlomi Fish
Hi Jeff,

On Wednesday 06 October 2010 13:26:00 Jeff Peng wrote:
  Hi folks,
  
  Can anyone please explain what is perlform and its
  
  usage.I used google but i was not able to crack.
 
 Try perldoc perlform.

In case you haven't read the book Perl Best Practices (which you should), 
you should know that http://search.cpan.org/dist/Perl6-Form/ would be 
preferable over the built-in perlform which has horrible behaviour.

Regards,

Shlomi Fish

 Cheers!

-- 
-
Shlomi Fish   http://www.shlomifish.org/
Parody on The Fountainhead - http://shlom.in/towtf

rindolf She's a hot chick. But she smokes.
go|dfish She can smoke as long as she's smokin'.

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: [Milan-pm] New note on page5notebook

2010-10-07 Thread marcos rebelo
the Idea is to do fast work, FAST. If I just need one exception, DBI
may do that to you.

I seen this pattern to often

eval {
   my $dbh = DBI-connect($dsn, $user, $password) or die $DBI::errstr;
   my $sth = $dbh-prepare(SELECT * FROM document WHERE id = ?) or
die $DBI::errstr;
   $sth-execute($id) // die $DBI::errstr;
   my $hash = $sth-fetchrow_hashref;
   die $DBI::errstr if $DBI::errstr;
   ...
};
if ($@) {
   ...
}

I prefer something like

eval {
   my $dbh = DBI-connect($dsn, $user, $password, {'RaiseError' = 1});
   my $sth = $dbh-prepare(SELECT * FROM document WHERE id = ?);
   my $rs = $sth-execute($id);
   my $hash = $sth-fetchrow_hashref;
   ...
};
if ($@) {
   ...
}

but for such a simple query, I would prefer

eval {
   my $ss = SmartSelect-new($dsn, $user, $password);
   my $hash = $ss-select_document_by_id($id)-[0];
   ...
};
if ($@) {
   ...
}

For having this in a production environment, I would have to develop
SmartSelect a little. Caching, more expressive queries, ... I didn't
found this in CPAN, I didn't look for it so much, but I would use it.

Like Einstein told: “Any fool can make things bigger, more complex,
and more violent. It takes a touch of genius-and a lot of courage-to
move in the opposite direction.”

Best Regards
Marcos Rebelo

2010/10/7 Gianluca Casati casati_gianl...@yahoo.it:

 I would use more $DBI::errstr after a prepare, an execute or a connect.
 Something like


 $dbh = DBI-connect( $NZ_SOURCE , $NZ_USER , $NZ_PASSWORD ) or die
 $log-abort(
 $connect_error_message . $DBI::errstr );

 other than that is a very comfortable approach, even if I prefere to keep
 queries in separate .sql files under a sql directory so other people that
 don't know Perl can edit them ( after that they accept the '?' special
 character :)

 Bye,

 see you the next meeting

 
 Da: marcos rebelo ole...@gmail.com
 A: milan...@pm.org; nl...@amsterdam.pm.org; perl-reci...@googlegroups.com;
 p...@lisbon.pm.org; Perl Begginers beginners@perl.org
 Inviato: Mar 5 ottobre 2010, 14:11:01
 Oggetto: [Milan-pm] New note on page5notebook

 Hi all

 I did one more note at:

 http://perl5notebook.oleber.com/objects/smart-selects-with-dynamic-response-to-undefiend-method-calls

 One example of the use of AUTOLOAD, to do some SQL dinamically.

 Comments are well come

 Best Regards
 Marcos Rebelo

 --
 Marcos Rebelo
 http://oleber.freehostia.com
 Milan Perl Mongers leader http://milan.pm.org
 Webmaster of http://perl5notebook.oleber.com
 ___
 Milan-pm mailing list
 milan...@pm.org
 http://mail.pm.org/mailman/listinfo/milan-pm


 ___
 Milan-pm mailing list
 milan...@pm.org
 http://mail.pm.org/mailman/listinfo/milan-pm





-- 
Marcos Rebelo
http://oleber.freehostia.com
Milan Perl Mongers leader http://milan.pm.org
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/




Re: [Milan-pm] New note on page5notebook

2010-10-07 Thread marcos rebelo
My idea is to throw away the SQL almost completely to 90% of the
queries. When the queries are a little bit more complex, I'd prefer to
create a View and use a simple interface.

I never did a Project to CPAN, but If someone is interested in growing
up the code I may help as much as I can +- 5 hours/week.

Best Regards
Marcos Rebelo

On Thu, Oct 7, 2010 at 12:08 PM, Oha o...@oha.it wrote:
 On 10/07/2010 11:27 AM, marcos rebelo wrote:

 but for such a simple query, I would prefer

 eval {
    my $ss = SmartSelect-new($dsn, $user, $password);
    my $hash = $ss-select_document_by_id($id)-[0];
    ...
 };
 if ($@) {
    ...
 }


 it's about 6 months i'm working on something like this, here a quick
 example:

        # obtain a single row from db
        my %row = select_uniq 'select * from foo where id = ?', $id;

        # when needed you may open a transaction
        tx_new
        {
                # if not differently specified, the default conenction will
 be used
                my %codes = select_map { $_{id} = $_{code} } 'SELECT * FROM
 codes';

                if($code{0}) {
                        tx_db 'master'; # going to use a specific connection

                        my $new = select_uniq 'SELECT max(id)+1 FROM codes';
                        tx_do 'UPDATE codes SET id = ? WHERE id = 0', $new;
                }
                # now the connection is again the default, cauz tx_db got out
 of scope

                # transaction may be nested, connection will be kept
 separated
                tx_new
                {
                        # if more then 1 rows is fetched, the following will
 confess
                        my %foo = select_uniq 'SELECT * FROM codes WHERE id =
 ?', 'foo';
                }
        } # connection will be committed and released, or rollbacked if an
 error is passing thru

 It is somewhat stable and i will probably put it on CPAN, but if you are
 interested i can share the code

 I'll gladly ear your thoughts about it.

 Oha
 ___
 Milan-pm mailing list
 milan...@pm.org
 http://mail.pm.org/mailman/listinfo/milan-pm




-- 
Marcos Rebelo
http://oleber.freehostia.com
Milan Perl Mongers leader http://milan.pm.org
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/




looking for suggestions

2010-10-07 Thread jm
all,

below is a sub i created to try to properly capitalize surnames of
irish/scottish descent, converting Macarthur = MacArthur, o'donnell
= O'Donnell, etc.

it works as intended but i was wondering if anyone can suggest
improvements in size and efficiency (realizing the two are not
necessarily compatible).
also rules for any additional naming styles would be appreciated since
the sites using this will have a fairly global audience, name-wise.

thanks in advance,
joe

#

print surname(NAME = $ARGV[0]) . \n;


#   SUB SURNAME
#   removes leading/trailing whitespace
#   consolidates grouped whitespaces into single whitespaces
#   capitalizes first letter after Mac/Mc/' in name (names of
Scottish/Irish descent)
#   capitalizes first letter of name upon return
sub surname
{
my %options = @_;
#   $options{NAME} = name to capitalize internally, if appropriate

#   remove leading and trailing whitespace, consolidate whitespace
groupings into single whitespaces
$options{NAME} = join(' ', split(' ', $options{NAME}));

if ($options{NAME} =~ /^M[a]*c|'/i)
{
$options{NAME} =~ m/c|'/g;
my $pos = pos $options{NAME};
substr($options{NAME}, $pos, 1) = uc(substr($options{NAME}, 
$pos, 1));
}   #   end of  if ($options{NAME} =~ /^M[a]*c|'/)

return(ucfirst($options{NAME}));
}   #   end of  sub surname

#
-- 
since this is a gmail account, please verify the mailing list is
included in the reply to addresses

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




Re: looking for suggestions

2010-10-07 Thread Shawn H Corey

On 10-10-07 02:08 PM, jm wrote:

it works as intended but i was wondering if anyone can suggest
improvements in size and efficiency


See `perldoc perlre` and search for /\\u/, /\\U/, /\\l/, and /\\L/.


--
Just my 0.0002 million dollars worth,
  Shawn

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 suggestions

2010-10-07 Thread John W. Krahn

jm wrote:

all,


Hello,


below is a sub i created to try to properly capitalize surnames of
irish/scottish descent, converting Macarthur =  MacArthur, o'donnell
=  O'Donnell, etc.

it works as intended but i was wondering if anyone can suggest
improvements in size and efficiency (realizing the two are not
necessarily compatible).
also rules for any additional naming styles would be appreciated since
the sites using this will have a fairly global audience, name-wise.

thanks in advance,
joe

#

printsurname(NAME =  $ARGV[0]) . \n;


#   SUB SURNAME
#   removes leading/trailing whitespace
#   consolidates grouped whitespaces into single whitespaces
#   capitalizes first letter after Mac/Mc/' in name (names of
Scottish/Irish descent)
#   capitalizes first letter of name upon return
sub surname
{
my %options = @_;


Do you really need a hash for a single value?  Why not just a scalar?



#   $options{NAME} = name to capitalize internally, if appropriate

#   remove leading and trailing whitespace, consolidate whitespace
groupings into single whitespaces
$options{NAME} = join(' ', split(' ', $options{NAME}));

if ($options{NAME} =~ /^M[a]*c|'/i)


Why the use of the character class with just one character?  /[a]*/ and 
/a*/ do exactly the same thing.  And does that mean that Maaac is 
valid because that is what the pattern matches.  Perhaps you want 
/^Ma?c|'/i.




{
$options{NAME} =~ m/c|'/g;


Why are you running another regular expression?  And why are you using 
the /g option?




my $pos = pos $options{NAME};
substr($options{NAME}, $pos, 1) = uc(substr($options{NAME}, 
$pos, 1));


What happens if $options{NAME} only contains Mac?



}   #   end of  if ($options{NAME} =~ /^M[a]*c|'/)

return(ucfirst($options{NAME}));
}   #   end of  sub surname

#





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

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




Re: looking for suggestions

2010-10-07 Thread jm
On Thu, Oct 7, 2010 at 1:22 PM, Shawn H Corey shawnhco...@gmail.com wrote:
 On 10-10-07 02:08 PM, jm wrote:

 it works as intended but i was wondering if anyone can suggest
 improvements in size and efficiency

 See `perldoc perlre` and search for /\\u/, /\\U/, /\\l/, and /\\L/.


 --
 Just my 0.0002 million dollars worth,
  Shawn

 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/





Shawn and John,

thanks, your leads gave me this:

#
#!/usr/bin/perl

print surname($ARGV[0]) . \n;


#   SUB SURNAME
#   removes leading/trailing whitespace
#   consolidates grouped whitespaces into single whitespaces
#   capitalizes first letter after Mac/Mc/' in name (names of
Scottish/Irish descent)
#   capitalizes first letter of name upon return
sub surname
{
my $name = shift;
$name = join(' ', split(' ', $name));
$name =~ s/(^[Mm]a?c|.')(.*)/\u$1\u$2/;
return(ucfirst($name));
}   #   end of  sub surname
#

John, to answer some of your questions:
the hash was legacy from earlier subs i've created, to allow for a
more generic structure.  i don't forsee that necessity here so i
changed to a scalar.
i also changed the first regex to use a?; not as comfortable with
regex's as i'd like yet.
the 2nd regex was required to allow the pos function to extract the
position of the desired character.  per the docs, the /g is a
requirement for pos (at least as i understand it).
since 'mac'  is ignored by the substitution (as is any other
'conventional' name) the ucfirst takes care of all those upon
return().

i'm thinking about trying to include the whitespace cleanup in the
s/// but i'm thinking it'll be an ugly piece of code i'll always have
trouble understanding.

again, thanks for your help, gentlemen.
joe

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




Re: looking for suggestions

2010-10-07 Thread Jim Gibson
On 10/7/10 Thu  Oct 7, 2010  12:20 PM, jm jm5...@gmail.com scribbled:


 
 Shawn and John,
 
 thanks, your leads gave me this:
 
 #
 #!/usr/bin/perl
 
 print surname($ARGV[0]) . \n;
 
 
 # SUB SURNAME
 # removes leading/trailing whitespace
 # consolidates grouped whitespaces into single whitespaces
 # capitalizes first letter after Mac/Mc/' in name (names of
 Scottish/Irish descent)
 # capitalizes first letter of name upon return
 sub surname
 {
 my $name = shift;
 $name = join(' ', split(' ', $name));
 $name =~ s/(^[Mm]a?c|.')(.*)/\u$1\u$2/;
 return(ucfirst($name));
 } # end of sub surname
 #
 


 i'm thinking about trying to include the whitespace cleanup in the
 s/// but i'm thinking it'll be an ugly piece of code i'll always have
 trouble understanding.

Use a separate regex instead of the join/split:

$name =~ s/\s+/ /g;

Not ugly. Easy to understand: substitute any substring of one or more
whitespace characters with a single space character.

Don't try to add this to your other regex. I am not sure that can even be
done. I am sure that it is not worth it.

Here is one perhaps more specific to your problem that may be a little
harder to understand:

$name =~ s/ {2,}/ /g;

That one will not substitute a single space with a single space, but you are
not likely to notice the difference in execution speed (if there even is
one). \s includes spaces, tabs, and newlines, so they are not exactly
equivalent.

Other possibilities:

$name =~ s/\s{2,}/ /g;
$name =~ s/[ ]{2,}/ /g;
$name =~ s/\s\s+/ /g;



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




Re: looking for suggestions

2010-10-07 Thread jm
On Thu, Oct 7, 2010 at 3:01 PM, Jim Gibson jimsgib...@gmail.com wrote:
 On 10/7/10 Thu  Oct 7, 2010  12:20 PM, jm jm5...@gmail.com scribbled:



 Shawn and John,

 thanks, your leads gave me this:

 #
 #!/usr/bin/perl

 print surname($ARGV[0]) . \n;


 # SUB SURNAME
 # removes leading/trailing whitespace
 # consolidates grouped whitespaces into single whitespaces
 # capitalizes first letter after Mac/Mc/' in name (names of
 Scottish/Irish descent)
 # capitalizes first letter of name upon return
 sub surname
 {
 my $name = shift;
 $name = join(' ', split(' ', $name));
 $name =~ s/(^[Mm]a?c|.')(.*)/\u$1\u$2/;
 return(ucfirst($name));
 } # end of sub surname
 #



 i'm thinking about trying to include the whitespace cleanup in the
 s/// but i'm thinking it'll be an ugly piece of code i'll always have
 trouble understanding.

 Use a separate regex instead of the join/split:

    $name =~ s/\s+/ /g;

 Not ugly. Easy to understand: substitute any substring of one or more
 whitespace characters with a single space character.

 Don't try to add this to your other regex. I am not sure that can even be
 done. I am sure that it is not worth it.

 Here is one perhaps more specific to your problem that may be a little
 harder to understand:

    $name =~ s/ {2,}/ /g;

 That one will not substitute a single space with a single space, but you are
 not likely to notice the difference in execution speed (if there even is
 one). \s includes spaces, tabs, and newlines, so they are not exactly
 equivalent.

 Other possibilities:

    $name =~ s/\s{2,}/ /g;
    $name =~ s/[ ]{2,}/ /g;
    $name =~ s/\s\s+/ /g;



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




jim,

thanks.  i'd initially considered separate regex's for whitespace but
decided on the join because it takes care of all whitespace
(leading/trailing/embedded) in one fell swoop.  i won't be trying to
combine the join with the existing regex; decided i'm not that much of
a glutton for punishment.

i actually did understand the {2,} so maybe i'm not as far out in the
cold as i'd feared :) .

i appreciate your insights and suggestions.
joe


-- 
since this is a gmail account, please verify the mailing list is
included in the reply to addresses

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




Re: looking for suggestions

2010-10-07 Thread Uri Guttman
 JG == Jim Gibson jimsgib...@gmail.com writes:

   i'm thinking about trying to include the whitespace cleanup in the
   s/// but i'm thinking it'll be an ugly piece of code i'll always have
   trouble understanding.

  JG Use a separate regex instead of the join/split:

  JG $name =~ s/\s+/ /g;

normally i would agree, but his split also deleted leading and trailing
whitespace since split ' ' has that special side effect. 

  JG Not ugly. Easy to understand: substitute any substring of one or more
  JG whitespace characters with a single space character.

even better and faster is to use tr/ //s (assuming only spaces and not
tabs/newlines, etc).

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: looking for suggestions

2010-10-07 Thread John W. Krahn

jm wrote:


Shawn and John,

thanks, your leads gave me this:

#
#!/usr/bin/perl

printsurname($ARGV[0]) . \n;


#   SUB SURNAME
#   removes leading/trailing whitespace
#   consolidates grouped whitespaces into single whitespaces
#   capitalizes first letter after Mac/Mc/' in name (names of
Scottish/Irish descent)
#   capitalizes first letter of name upon return
sub surname
{
my $name = shift;
$name = join(' ', split(' ', $name));
$name =~ s/(^[Mm]a?c|.')(.*)/\u$1\u$2/;
return(ucfirst($name));
}   #   end of  sub surname
#

John, to answer some of your questions:
the hash was legacy from earlier subs i've created, to allow for a
more generic structure.  i don't forsee that necessity here so i
changed to a scalar.
i also changed the first regex to use a?; not as comfortable with
regex's as i'd like yet.
the 2nd regex was required to allow the pos function to extract the
position of the desired character.  per the docs, the /g is a
requirement for pos (at least as i understand it).


You could use the @+ and @- arrays to find the start and end position of 
a regular expression.


perldoc perlvar



since 'mac'  is ignored by the substitution (as is any other
'conventional' name) the ucfirst takes care of all those upon
return().

i'm thinking about trying to include the whitespace cleanup in the
s/// but i'm thinking it'll be an ugly piece of code i'll always have
trouble understanding.




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

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




Finding characters from the file encoded with - ISO8859_1

2010-10-07 Thread Parag Kalra
Hi All,

I have a file encoded with ISO8859_1

I wish to find and print all the characters from a certain range say between
224 and 255

ord function doesn't seem to work here. Any other work around.

Cheers,
Parag


Re: looking for suggestions

2010-10-07 Thread Shlomi Fish
On Thursday 07 October 2010 21:20:19 jm wrote:
 On Thu, Oct 7, 2010 at 1:22 PM, Shawn H Corey shawnhco...@gmail.com wrote:
  On 10-10-07 02:08 PM, jm wrote:
  it works as intended but i was wondering if anyone can suggest
  improvements in size and efficiency
  
  See `perldoc perlre` and search for /\\u/, /\\U/, /\\l/, and /\\L/.
  
  
  --
  Just my 0.0002 million dollars worth,
   Shawn
  
  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/
 
 Shawn and John,
 
 thanks, your leads gave me this:
 
 #
 #!/usr/bin/perl
 

Add strict and warnings.

 print surname($ARGV[0]) . \n;

Don't use leading-ampersand in subroutine calls. Also see:

http://perl-begin.org/tutorials/bad-elements/

Regards,

Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
Funny Anti-Terrorism Story - http://shlom.in/enemy

rindolf She's a hot chick. But she smokes.
go|dfish She can smoke as long as she's smokin'.

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: Finding characters from the file encoded with - ISO8859_1

2010-10-07 Thread John W. Krahn

Parag Kalra wrote:

Hi All,


Hello,


I have a file encoded with ISO8859_1

I wish to find and print all the characters from a certain range say between
224 and 255


print $string =~ /[\xE0-\xFF]/g;




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

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




Re: Finding characters from the file encoded with - ISO8859_1

2010-10-07 Thread Parag Kalra
By range I mean: http://htmlhelp.com/reference/charset/iso224-255.html

Cheers,
Parag



On Thu, Oct 7, 2010 at 2:17 PM, Parag Kalra paragka...@gmail.com wrote:

 Hi All,

 I have a file encoded with ISO8859_1

 I wish to find and print all the characters from a certain range say
 between 224 and 255

 ord function doesn't seem to work here. Any other work around.

 Cheers,
 Parag




Perl Elements to Avoid Document on http://perl-begin.org/

2010-10-07 Thread Shlomi Fish
Hi all,

after being tired of telling Perl newcomers about the same problems with their 
code times and times again, I've decided to create this page detailing Perl 
Elements to avoid:

http://perl-begin.org/tutorials/bad-elements/

I've already mentioned it several times here in replies, but thought I'll 
create a top-level thread for discussion.

Here's my to-do list for that page. More ideas are welcome:

[quote]

* Add to the page about best practices:
- discuss commenting.
- refactoring:
- magic numbers.
- duplicate code.
- long functions and methods.
- accessing object slots directly
- use accessors.
- EEK 
- pass items from one subroutine to another.
- use objects.
- assign them to slots.
- ^ and $ in regexes.
- ambiguous.
- better use \A and \z to mean start-of-string and end-of-string.
- with /m can be used for start-of-line / end-of-line.
- Law of Demeter
- Mixing tabs and spaces.
- Always add an explicit return from the subroutine.
- leading underscores (_) for non-API methods and functions.
- print {$fh} @args instead of print $fh @args which is too easy to 
miss.
- varvarname - using a variable as a different variable's name.
- http://www.stonehenge.com/merlyn/UnixReview/col52.html
- $object-new() to create a new instance.
- avoid using perlform - use Perl6::Form instead (also see the text
generation page).
- don't overuse $_ - it's prune to errors.
- C-style for loops.
- Always label your loops.
- Accessing the last element using $array[$#array] - use $array[-1]
instead.
- Array element: @array[$idx] instead of $array[$idx].
- modifying an array/hash while iterating over it.
[/quote]

Regards,

Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
List of Portability Libraries - http://shlom.in/port-libs

rindolf She's a hot chick. But she smokes.
go|dfish She can smoke as long as she's smokin'.

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/




Auto completing STDIN from a script

2010-10-07 Thread Chris E. Rempola
I am making a Perl script that needs to execute another Perl script. 
The second script is sort of like a verification thing before it does 
it's thing and it asks to confirm some details.


For example, the second script will prompt to enter your name which it 
grabs STDIN.


Can someone point or tell me what to do in the first script to kind of 
fake STDIN.  So when prompted, will run the second script and 
auto-complete.  Any suggestions?  Thanks.


-Chris

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




Re: Perl Elements to Avoid Document on http://perl-begin.org/

2010-10-07 Thread Paul Johnson
On Fri, Oct 08, 2010 at 12:07:30AM +0200, Shlomi Fish wrote:

 after being tired of telling Perl newcomers about the same problems with 
 their 
 code times and times again, I've decided to create this page detailing Perl 
 Elements to avoid:
 
 http://perl-begin.org/tutorials/bad-elements/
 
 I've already mentioned it several times here in replies, but thought I'll 
 create a top-level thread for discussion.
 
 Here's my to-do list for that page. More ideas are welcome:

Hello Shlomi,

Although it should probably go without saying, please be sure to mention
somewhere near the top that this is your opinion.  Whilst many people
will agree with much of what you have written, or will write, I suspect
that hardly anyone, and perhaps only you yourself, will agree with
everything.  Indeed, in some instances it is likely that some people
will consider that you are giving bad advice.

It might also be useful to note that you are providing guidelines for
beginners, and that people should feel free to go against this advice
when they understand why they are doing so.

Making such implicit caveats explicit could help to soften the tone and
avoid confusion.

I've CCed this to the list because it probably also applies to the
majority of the cases where somone sends a black and white reply to
the list which isn't solving a specific problem (Don't do that, do
this, or always ...).

Perl is used for many different tasks, and often different problems will
suggest correspondingly different solutions.  Or perhaps someone just
has a different style.  Pointing out and correcting mistakes is, of
course, always welcome on the list, but it does seem that more and more
frequently we are straying into the area of style, and here, greater
care is required.

Thanks,

-- 
Paul Johnson - p...@pjcj.net
http://www.pjcj.net

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




Re: Auto completing STDIN from a script

2010-10-07 Thread Robert Wohlfarth
On Wed, Oct 6, 2010 at 7:24 PM, Chris E. Rempola ch...@bayarea.net wrote:

 I am making a Perl script that needs to execute another Perl script. The
 second script is sort of like a verification thing before it does it's thing
 and it asks to confirm some details.

 For example, the second script will prompt to enter your name which it
 grabs STDIN.

 Can someone point or tell me what to do in the first script to kind of fake
 STDIN.  So when prompted, will run the second script and auto-complete.  Any
 suggestions?  Thanks.


Take a look at http://perldoc.perl.org/perlopentut.html#Pipe-Opens.

-- 
Robert Wohlfarth


Re: Perl Elements to Avoid Document on http://perl-begin.org/

2010-10-07 Thread John W. Krahn

Shlomi Fish wrote:

Hi all,


Hello,


after being tired of telling Perl newcomers about the same problems with their
code times and times again, I've decided to create this page detailing Perl
Elements to avoid:

http://perl-begin.org/tutorials/bad-elements/


QUOTE
the ultimately wrong, insecure and/or outdated styles are:

# Bareword filehandle (type glob), two arguments open (insecure) and no
# error handling
open INPUT, $filename;
/QUOTE

You say outdated styles implying more than one example but you don't 
show the other style i.e.:


use vars qw/ $INPUT /;
our $INPUT = $filename;
open INPUT;

You say wrong and insecure which is incorrect if the file name is 
prefixed and postfixed correctly, as described in the documentation.


$filename = ./$filename\0;


QUOTE
Make sure you never use bareword here documents EOF which are valid 
syntax, but people are never sure whether they are EOF or `EOF`.

/QUOTE

I am a people and I am very sure that EOF is exactly the same as 
EOF.  And EOF is usually short for End-Of-File so why use it for a 
string delimiter?



QUOTE
Slurping a file (i.e: Reading it all into memory)
/QUOTE

You are missing some other examples:

read $fh, my $contents, -s $fh;

sysread $fh, my $contents, -s $fh;


QUOTE
sub my_func
{
my ($prefix, $array_ref) = @_;

foreach my $item (@{$array_ref})
{
print $prefix, : , $item, \n;
}

return;
}
/QUOTE

Why use a loop to print a list?

sub my_func
{
my ($prefix, $array_ref) = @_;

print $prefix, join( : , @$array_ref ), \n;

return;
}


QUOTE
An arbitrary C-style loop can be replaced with a while loop with a 
continue block.

/QUOTE

For example?




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

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