Re: Unfamiliar syntax

2018-07-20 Thread Octavian Rasnita
From: James Kerwin 
  Afternoon all,


  I have been asked to take a look at a .pl file which is part of a set-up 
called "EPrints". The particular file controls who can access documents on a 
server.


  Excluding some comments, the file starts like:

  $c->{can_request_view_document} = sub
  {
  my( $doc, $r ) = @_;


  It then goes on to complete the subroutine and return a value.


  I understand that $doc and $r are populated via @_ but I have never before 
seen the structure of the first line starting with "$c". Additionally, the file 
doesn't look like a typical Perl file (eg.#!/usr/bin/perl -w at the top and 
some use statements).


  I know it's very vague, but could anybody explain this syntax to me? My 
suspicion is that something else is calling the subroutines in this file? Am I 
at least on the right track?


  Thanks,
  James


  Hi, James

  Yes, you are on the right track.

  This is an annonymous subroutine which is probably used as a callback 
subroutine somewhere else.

  You can define such subroutines using a code like:

  my $my_sub = sub { ... }

  Then you can use the $my_sub  variable as any other scalar variables, for 
example as a parameter in another subroutine call, like:

  do_something( $my_sub );

  ...then the sub do_something could use this sub like:

  sub do_something {
  my ( $sub ) = @_;
  $sub->(); #execute the callback subroutine here
  }

  If this subroutine accepts parameters, you can call it like:

  $my_sub->( $doc, $r );

  Now, in your case, you don't have a scalar variable $my_sub to store this 
subroutine, but you have another scalar value of a hashref $c, stored in the 
key can_request_view_document, which is $c->{can_request_view_document},.

  So in order to execute this sub, you do:

  $c->{can_request_view_document}->( $doc, $r );

  The code is common Perl code. It doesn't matter that the file doesn't start 
with #!/usr/bin/perl
  The shebang line is not needed if the program is executed with a command like:
  perl program.pl

  --Octavian


Re: Unfamiliar syntax

2018-07-20 Thread Илья Рассадин

I Found it on github

https://github.com/eprints/eprints/blob/392474eec1b8125a66ed2d3e12b02aeb67dc07c4/lib/defaultcfg/cfg.d/security.pl


On 7/20/18 6:24 PM, Chas. Owens wrote:
All of this is supposition since I can't see anything you haven't 
shown us.


It sounds like this code is part of a larger program that is going to 
call


   do "EPrints";

which will bring the source of EPrints into the larger program. The $c 
variable is probably setup there.  What the code in EPrints is doing 
is setting up a handler named can_request_view_document.  At some 
point in the larger program, it is going to say something like


my $document = $r->param(doc);
my $can_request = $c->{can_request_view_document}->($document, $r);

return 403, "You are not allowed to access $document" unless $can_request;
return 400, "$document doesn't exist" unless -f $document;
return 200, $document;


On Fri, Jul 20, 2018 at 11:06 AM James Kerwin > wrote:


Afternoon all,

I have been asked to take a look at a .pl file which is part of a
set-up called "EPrints". The particular file controls who can
access documents on a server.

Excluding some comments, the file starts like:

$c->{can_request_view_document} = sub
{
    my( $doc, $r ) = @_;

It then goes on to complete the subroutine and return a value.

I understand that $doc and $r are populated via @_ but I have
never before seen the structure of the first line starting with
"$c". Additionally, the file doesn't look like a typical Perl file
(eg.#!/usr/bin/perl -w at the top and some use statements).

I know it's very vague, but could anybody explain this syntax to
me? My suspicion is that something else is calling the subroutines
in this file? Am I at least on the right track?

Thanks,
James



Re: Unfamiliar syntax

2018-07-20 Thread Илья Рассадин

Hi!

$c is hash reference with key "can_request_view_document";

The value for that key is anonymous sub.

You can call this sub like this

$c->{can_request_view_document}->($doc, $r);



On 7/20/18 6:04 PM, James Kerwin wrote:

Afternoon all,

I have been asked to take a look at a .pl file which is part of a 
set-up called "EPrints". The particular file controls who can access 
documents on a server.


Excluding some comments, the file starts like:

$c->{can_request_view_document} = sub
{
    my( $doc, $r ) = @_;

It then goes on to complete the subroutine and return a value.

I understand that $doc and $r are populated via @_ but I have never 
before seen the structure of the first line starting with "$c". 
Additionally, the file doesn't look like a typical Perl file 
(eg.#!/usr/bin/perl -w at the top and some use statements).


I know it's very vague, but could anybody explain this syntax to me? 
My suspicion is that something else is calling the subroutines in this 
file? Am I at least on the right track?


Thanks,
James


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




Re: Unfamiliar syntax

2018-07-20 Thread Chas. Owens
All of this is supposition since I can't see anything you haven't shown us.

It sounds like this code is part of a larger program that is going to call

   do "EPrints";

which will bring the source of EPrints into the larger program. The $c
variable is probably setup there.  What the code in EPrints is doing is
setting up a handler named can_request_view_document.  At some point in the
larger program, it is going to say something like

my $document = $r->param(doc);
my $can_request = $c->{can_request_view_document}->($document, $r);

return 403, "You are not allowed to access $document" unless $can_request;
return 400, "$document doesn't exist" unless -f $document;
return 200, $document;


On Fri, Jul 20, 2018 at 11:06 AM James Kerwin  wrote:

> Afternoon all,
>
> I have been asked to take a look at a .pl file which is part of a set-up
> called "EPrints". The particular file controls who can access documents on
> a server.
>
> Excluding some comments, the file starts like:
>
> $c->{can_request_view_document} = sub
> {
> my( $doc, $r ) = @_;
>
> It then goes on to complete the subroutine and return a value.
>
> I understand that $doc and $r are populated via @_ but I have never before
> seen the structure of the first line starting with "$c". Additionally, the
> file doesn't look like a typical Perl file (eg.#!/usr/bin/perl -w at the
> top and some use statements).
>
> I know it's very vague, but could anybody explain this syntax to me? My
> suspicion is that something else is calling the subroutines in this file?
> Am I at least on the right track?
>
> Thanks,
> James
>


Re: Unfamiliar syntax

2018-07-20 Thread Gil Magno
2018-07-20 16:04:11 +0100 James Kerwin:
> Afternoon all,
> 
> I have been asked to take a look at a .pl file which is part of a set-up
> called "EPrints". The particular file controls who can access documents on
> a server.
> 
> Excluding some comments, the file starts like:
> 
> $c->{can_request_view_document} = sub
> {
> my( $doc, $r ) = @_;
> 
> It then goes on to complete the subroutine and return a value.
> 
> I understand that $doc and $r are populated via @_ but I have never before
> seen the structure of the first line starting with "$c". Additionally, the
> file doesn't look like a typical Perl file (eg.#!/usr/bin/perl -w at the
> top and some use statements).

Maybe some piece of code is doing "do 'the_file.pl'" or the file's
content is being given to eval(). In these cases, probably a hashref
$c was already declared and being used in the code that runs the do()
or eval().

> I know it's very vague, but could anybody explain this syntax to me? My
> suspicion is that something else is calling the subroutines in this file?
> Am I at least on the right track?
> 
> Thanks,
> James


signature.asc
Description: Digital signature


Unfamiliar syntax

2018-07-20 Thread James Kerwin
Afternoon all,

I have been asked to take a look at a .pl file which is part of a set-up
called "EPrints". The particular file controls who can access documents on
a server.

Excluding some comments, the file starts like:

$c->{can_request_view_document} = sub
{
my( $doc, $r ) = @_;

It then goes on to complete the subroutine and return a value.

I understand that $doc and $r are populated via @_ but I have never before
seen the structure of the first line starting with "$c". Additionally, the
file doesn't look like a typical Perl file (eg.#!/usr/bin/perl -w at the
top and some use statements).

I know it's very vague, but could anybody explain this syntax to me? My
suspicion is that something else is calling the subroutines in this file?
Am I at least on the right track?

Thanks,
James