Página 1 de 1

File::Find error

NotaPublicado: 2012-07-03 09:23 @433
por danimera
Bueno, no sé si sea un error, pero tengo dentro de mi clase CGI-App dos métodos uno para crear páginas y otro para editar.

Pero no logro mostrar el contenido de un directorio a la hora de crear un nuevo contenido, pero cuando lo voy a editar sí me lista el directorio...

Y los métodos son similares entre sí: uno es para crear un contenido, y otro para editar el contenido...


Este es el que no me imprime los @dirs en la línea 39:

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. sub content_add_form {
  2.     my $self = shift;
  3.     my $dbh = $self->dbh;
  4.     my $language = $self->current_language();
  5.  
  6.     my $contenido = MyApp::Db::Content->new($dbh);
  7.  
  8.         my $fck = CGI::FCKeditor->new();
  9.  
  10.     $fck->set_name('content');        #HTML <input name>(default 'fck')
  11.     $fck->set_base('../js/fckeditor/');        #FCKeditor Directory
  12.     $fck->set_set('Default');       #FCKeditor Style(Basic 'Default')
  13.     $fck->set_height('350');
  14.     $fck->set_value('Cree su contenido');
  15.         my $fk = $fck->fck;    #output html source
  16.  
  17.         my $form = CGI::FormBuilder->new(
  18.             name     => 'form_contenido',
  19.             fields => $contenido->{fields},
  20.             method   => 'post',
  21.             fieldset => 'Info',
  22.             action   => 'content.pl?mode=save',
  23.             stylesheet =>  "http://localhost/css/jquery-ui-1.8.4.custom.css",
  24.             template => "templates/".$language.'/contenido_form.html',
  25.             required => [qw(title content)]
  26.         );
  27.  
  28.     #======================================================================
  29.    #======================================================================
  30.     # LEEMOS LA CARPETA DE TEMPLATES
  31.     #======================================================================
  32.  
  33.     my $some_dir = $self->cfg('templates_dir')."/$language/".$self->cfg('template_name');
  34.  
  35.     find( \&w , $some_dir);
  36.     sub w{
  37.         if( -f "$some_dir/$_" ){push (@dirs,$_);}
  38.     }
  39.     die Dumper @dirs;
  40.     #======================================================================
  41.     #die $some_dir;
  42.     #Poner el fck editor
  43.     $form->tmpl_param(fck  =>  $fk);
  44.  
  45.     $form->field(name => 'mode', type => 'hidden', value=>'save');
  46.         $form->field(name => 'title', label => 'Titulo', size=>"50");
  47.     $form->field(name => 'text_menu', label => 'Texto Menu');
  48.  
  49.     #$form->field(name => 'content', label => 'Contenido', type=> 'textarea' );
  50.  
  51.     $form->field(name => 'tags', label => 'Last Name', size => 50);
  52.     $form->field(name => 'decription', label => 'Descripcion', size  => 50);
  53.     #$form->field(name => 'image', type => 'file', disabled => 1);
  54.     $form->field(name => 'template', options => \@dirs, value => "template.html" );
  55.     $form->field(name => 'status', options => [ qw(Publicado Inactivo)]);
  56.     $form->field(name => 'show_in_menu', options => [ qw(SI NO)]);
  57.     $form->field(name => 'target', options => [ qw(_self _blank)]);
  58.  
  59.     if ($form->submitted && $form->validate) {
  60.           return $self->content_add_process($form);
  61.     }
  62.  
  63.     #return $form->render;
  64.     return $self->display($form->render(),'','text');
  65.  
  66. }
  67.  
Coloreado en 0.004 segundos, usando GeSHi 1.0.8.4


Y este otro sí me imprime los directorios:

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. sub content_edit_form {
  2.         my $self = shift;
  3.     my $dbh = $self->dbh;
  4.     my $q = $self->query();
  5.     my $language = $self->current_language();
  6.  
  7.     my $contenido = MyApp::Db::Content->new($dbh);
  8.  
  9.     $contenido->retrieve($q->param('id'));
  10.  
  11.         my $fck = CGI::FCKeditor->new();
  12.  
  13.     $fck->set_name('content');        #HTML <input name>(default 'fck')
  14.     $fck->set_base('../js/fckeditor/');        #FCKeditor Directory
  15.     $fck->set_set('Default');       #FCKeditor Style(default 'Default')
  16.     $fck->set_height('350');
  17.     $fck->set_value($contenido->{data}->{content});
  18.         my $fk = $fck->fck;    #output html source
  19.  
  20.         my $form = CGI::FormBuilder->new(
  21.             name     => 'form_contenido',
  22.             fields => $contenido->{fields},
  23.             method   => 'post',
  24.             fieldset => 'Info',
  25.             action   => 'content.pl?mode=update',
  26.             stylesheet => $self->cfg('site_url')."/css/jquery-ui-1.8.4.custom.css",
  27.             template => "templates/".$language.'/contenido_form.html',
  28.             required => [qw(title content)]
  29.         );
  30.     #======================================================================
  31.    #======================================================================
  32.     # LEEMOS LA CARPETA DE TEMPLATES
  33.     #======================================================================
  34.  
  35.     my $some_dir = $self->cfg('templates_dir')."/$language/".$self->cfg('template_name');
  36.  
  37.     find( \&w , $some_dir);
  38.     sub w{
  39.         if( -f "$some_dir/$_" ){push (@dirs,$_);}
  40.     }
  41.     die Dumper @dirs;
  42.     #======================================================================
  43.  
  44.     #Poner el fck editor
  45.     $form->tmpl_param(fck  =>  $fk);
  46.     $form->field(name => 'mode', type => 'hidden', value=>'update');
  47.     $form->field(name => 'id', type => 'hidden', value=>$q->param('id'));
  48.         $form->field(name => 'title', label => 'Titulo', size => '60', value => $contenido->{data}->{title});
  49.     $form->field(name => 'text_menu', label => 'Texto Menu', value => $contenido->{data}->{text_menu});
  50.     #$form->field(name => 'content', label => 'Contenido', type=> 'textarea' );
  51.     $form->field(name => 'tags', label => 'Last Name', size => 50,  value => $contenido->{data}->{tags});
  52.     $form->field(name => 'decription', label => 'Descripcion', size  => 50, value => $contenido->{data}->{decription});
  53.     $form->field(name => 'image', type => 'file', disabled => 1, value => $contenido->{data}->{image});
  54.  
  55.     $form->field(name => 'template', options => \@dirs, value => $contenido->{data}->{template} );
  56.     #$form->field(name => 'template', options => \@dirs, value => "template.html" );
  57.  
  58.  
  59.     $form->field(name => 'status', options => [ qw(Publicado Inactivo)], value => $contenido->{data}->{status});
  60.     $form->field(name => 'show_in_menu', options => [ qw(SI NO)], value => $contenido->{data}->{show_in_menu});
  61.     $form->field(name => 'target', options => [ qw(_self _blank)], value => $contenido->{data}->{target});
  62.  
  63.     if ($form->submitted && $form->validate) {
  64.  
  65.  
  66.           return $self->content_edit_process($form);
  67.  
  68.     }
  69.  
  70.     #return $form->render;
  71.     return $self->display($form->render(),'','text');
  72.  
  73.  
  74.  
  75. }
  76.  
Coloreado en 0.003 segundos, usando GeSHi 1.0.8.4


Los data dumpers para el segundo me arroja:

Sintáxis: [ Descargar ] [ Ocultar ]
  1. $VAR1 = 'news.html'; $VAR2 = 'cart_widget_display.html'; $VAR3 = 'template_2.html'; $VAR4 = 'store_display.html'; $VAR5 = 'content.html'; $VAR6 = 'cart_display.html'; $VAR7 = 'store.html'; $VAR8 = 'template_1.html'; $VAR9 = 'item_display.html'; $VAR10 = 'gallery.html'; $VAR11 = 'news_display.html'; $VAR12 = 'gallery_display.html'; $VAR13 = 'home.html'; $VAR14 = 'template.html';  


Y para el primero no me arroja nada :S Estoy confundido. No sé qué haya hecho mal.

Re: File::Find error

NotaPublicado: 2012-07-03 11:29 @520
por explorer
No vemos la declaración de @dirs, pero, aparte de eso, si el código es el mismo, quizás la diferencia esté en $some_dir.

Prueba a sacar el valor de $some_dir después de la línea 33, y verifica que contiene una ruta correcta.

Por otra parte, para sacar los archivos de un directorio no necesitas usar el File::Find (salvo que además quieras hacer una búsqueda en profundidad). Te vale con un simple glob():
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. use File::Basename;
  2. my @archivos = map { basename $_ } glob $some_dir;
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4


O incluso mejor: un readdir():
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. opendir DIR, $some_dir;
  2. my @archivos = grep { !/^\./ } readdir DIR;
  3. closedir DIR;
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4

Re: File::Find error

NotaPublicado: 2012-07-03 11:34 @524
por danimera
En el sub de edición tengo
$VAR1 = '/home1/talabary/public_html/templates/spanish/talabary';

y en adicionar tengo

$VAR1 = '/home1/talabary/public_html/templates/spanish/talabary';


Uso file-find porque con readdir() tampoco me trae nada, claro que con readdir sí me recorre los listados pero no me trae los nombres con $_.

La misma aplicación funciona local y en otro server, sin cambio y corre normal. Quiero solo listar los files de ese directorio pero tengo problemas en este hosting... :S

Perdón, con readdir() sí pude leer el directorio, ahora sin problemas, en el método add. Y en el método edit lo leo con file-find.

Re: File::Find error

NotaPublicado: 2012-07-03 11:55 @538
por explorer
Si puedes hacerlo todo con readdir(), mejor, porque es muchísimo más rápido que File::Find (pues hace muchas más operaciones).

Pero sigue siendo un misterio el porqué funcionaba de forma distinta.

En cuanto a que si readdir() devuelve algo en $_, no debería importarte si recoges todos los archivos de la forma en que te lo puse (directamente al array, filtrando los archivos/directorios que empiecen por '.').

Un detalle: escribir un simple readdir() dentro de un while(), solo funciona correctamente a partir de Perl v5.12 :)

Re: File::Find error

NotaPublicado: 2012-07-03 12:02 @543
por danimera
La verdad no me importa como devuelta los datos, pero si me importa el entender por qué. No funcionan las cosas cuando estoy seguro de que debería funcionar. Ahora tenemos:

Los hosting generalmente tiene la versión 5.8 de Perl, y yo usaba dentro de while() el readdir.

Ahora uso:

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. opendir DIR, $some_dir;
  2.  
  3. my @archivos = grep { !/^\./ } readdir DIR;
  4.  
  5. closedir DIR;
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4


Y ya no tengo problemas, además me ahorro un módulo.