Saturday 15 May 2010

perl - How can I write readable subroutine hash arguments? -


At work, we prefer to argue in the subreinit as a hash ref:

  My $ Manager = DatabaseManager-> New ({'sHost' = & gt; 'my_host', 'd database' = & gt; 'some_database'});   

The object has an init function that looks like this:

  sub_nitt {my ($ self, $ phArgs) = @_; $ Self- & gt; {_ SHost} = $ phArgs-> {Eshost}; $ Self- & gt; {_ SDatabase} = $ phArgs-> {SDatabase}; }   

Is there any way to clean it? Even if only the first line can be clear, I think that this will be a correction.

  sub_nit {My ($ self, (host, $ database)) = @_ ; $ Self- & gt; {_ SHost} = $ host; $ Self- & gt; {_ SDatabase} = $ database; }   

Then it is clear that what substrate requires is any idea?

It looks like you want the "name" option. I can do something like this:

  my ($ self, $ phArgs) = @_; Mine ($ sHost, $ sDatabase) = @ {$ phArgs} {qw (sHost sDatabase)};   

But then you mention sHost and sDatabase four times because you

  $ Self-> {_ SHost} = $ sHost; $ Self- & gt; {_ SDatabase} = $ sDatabase;   

You can not do anything about it, because this is especially what you want. I think this is crazy to consider the whole sub: can be written as:

  My ($ self, $ phArgs) = @_; @ $ Self {qw (_sHost sDatabase)} = @ $ phArgs {qw (sHost sDatabase)};   

or

  My ($ self, $ phArgs) = @_; $ Self-> {"_". $ _} = $ PhArgs-> {$ _} For qw (sHost sDatabase);    

No comments:

Post a Comment