Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Considering Prototypes

by chunlou (Curate)
on Jul 28, 2003 at 18:42 UTC ( #278555=note: print w/ replies, xml ) Need Help??


in reply to Considering Prototypes

It's true that lack of "strict function-argument specification" or its "signature-less" subroutines could be annoying at times.

It's useful however when you need high level of abstraction. Consider the following pseudo self contained code excerpt:

#! /usr/local/bin/perl -w use strict ; # ################################################################# # Schema: Survey Database my %Survey = ( tblq => { primID => ['q_id'], cols => ['anstmpl_id', 'short_label', 'question' +, 'updated'] }, tblqtmpl => { primID => ['qtmpl_id'], cols => ['name', 'descr', 'updated'] }, tblqtmpl_q => { primID => ['qtmpl_id', 'q_id'], cols => ['seq', 'notes', 'updated'] }, ); # ################################################################# # Package: Survey Database {package Survey ; # assume %Survey defined somewhere # = = = = = = = = = = = = = = = = Contructor = = = = = = = = = = = + = = = = = = # sub dbconnect { } # = = = = = = = = = = = = = = = Public Methods = = = = = = = = = = + = = = = = = # foreach my $tbl (keys %Survey){ # define subroutines <action>_<tab +le> at runtime eval qq/sub insert_$tbl {return (shift)->_insert_table(_tblnam +e(),\@_)}/ ; eval qq/sub update_$tbl {return (shift)->_update_table(_tblnam +e(),\@_)}/ ; eval qq/sub delete_$tbl {return (shift)->_delete_table(_tblnam +e(),\@_)}/ ; } # = = = = = = = = = = = = = = = Private Methods = = = = = = = = = += = = = = = =# sub _insert_table { my ($self, $tbl, $values) = (shift, shift, shift) ; # ... and more... } sub _update_table { my ($self, $tbl, $values) = (shift, shift, shift) ; # ... and more... } sub _delete_table { my ($self, $tbl, $values) = (shift, shift, shift) ; # ... and more... } # -------------------------------------------------------------- sub _tblname{ # return <tbl>, if called by <action>_<tbl> e.g. s +how_tblanstype ( my $sub = (caller(1))[3] ) =~ s/.*::.*?_(.*)/$1/ ; return $sub ; } } # ################################################################# # Test Script: Survey Database Package # tblq $Survey->insert_tblq(\%data) ; $Survey->update_tblq(\%data) ; $Survey->delete_tblq(\%data) ; # tblqtmpl $Survey->insert_tblqtmpl(\%data) ; $Survey->update_tblqtmpl(\%data) ; $Survey->delete_tblqtmpl(\%data) ; # tblqtmpl_q $Survey->insert_tblqtmpl_q(\%data) ; $Survey->update_tblqtmpl_q(\%data) ; $Survey->delete_tblqtmpl_q(\%data) ;

The focus is such lines as eval qq/sub insert_$tbl {return (shift)->_insert_table(_tblname(),\@_)}/;. It generates subroutines at compile time. Having strict function argument specification may not allow you such a coding. Not necessarily good or bad, just another way to do things.


______________________
Footnote: As a side question, why not something likeinsert($table, $data) instead of insert_thistable($data)? Well, if you're refactoring existing code and some other scripts have been using insert_thistable($data), you have no choice but keep the interface.


Comment on Re: Considering Prototypes
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://278555]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (12)
As of 2015-07-07 21:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls